惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Vulnerabilities – Threatpost
U
Unit 42
F
Fortinet All Blogs
aimingoo的专栏
aimingoo的专栏
P
Proofpoint News Feed
F
Full Disclosure
月光博客
月光博客
Engineering at Meta
Engineering at Meta
博客园_首页
The Register - Security
The Register - Security
G
Google Developers Blog
The Cloudflare Blog
博客园 - Franky
K
Kaspersky official blog
A
Arctic Wolf
Scott Helme
Scott Helme
C
Cisco Blogs
Hugging Face - Blog
Hugging Face - Blog
C
Check Point Blog
NISL@THU
NISL@THU
AI
AI
D
DataBreaches.Net
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Stack Overflow Blog
Stack Overflow Blog
Project Zero
Project Zero
The GitHub Blog
The GitHub Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
量子位
Vercel News
Vercel News
T
Tor Project blog
P
Privacy International News Feed
D
Docker
I
Intezer
L
LangChain Blog
P
Proofpoint News Feed
Security Latest
Security Latest
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threatpost
博客园 - 聂微东
AWS News Blog
AWS News Blog
Martin Fowler
Martin Fowler
P
Privacy & Cybersecurity Law Blog
V
V2EX
Last Week in AI
Last Week in AI
C
Cybersecurity and Infrastructure Security Agency CISA
The Hacker News
The Hacker News
T
Tenable Blog
Blog — PlanetScale
Blog — PlanetScale
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog

博客园 - Flymouse

Error CS0006 when invoking MSBuild from command line 一个Cookie引起的混乱 数据库错误:在执行批处理时出现错误。错误消息为: 目录名无效 NorthScale MemBase Server 1.6 的使用 使用网络目录映射虚拟目录出现 500.19 权限不足问题的一个解决办法 URL重写给 asp.net Ajax带来的问题 调整viewState的位置,有助于SEO 关于限制水晶报表的导出格式 2个Excel异常处理 Win7 x64 IIS7支持32位应用程序 在.net中与mysql数据库的时候碰到显示不了中文字符的情况实现! 完美解决IFRAME中COOKIE、SESSION丢失 关于Ucenter在IIS中有时出现“Access denied for agent changed”错误。 如何將 Access 的 Memo 型態欄位匯入到 SQL2005 的 nvarchar 型態欄位 .net wap强制输出WML - Flymouse - 博客园 使用Extjs的Form无法输入的问题 jQuery滚屏代码,还有一点地方封装不进去 - Flymouse - 博客园 ASP.NET 2.0 中动态添加 GridView 模板列的例子 关于使用 jquery Validate 使用出现的问题
C# Excel进程关闭 - Flymouse - 博客园
Flymouse · 2010-05-08 · via 博客园 - Flymouse
using   System.Runtime.InteropServices;     
        
  [DllImport("User32.dll",   CharSet   =   CharSet.Auto)]     
  public   static   extern   int   GetWindowThreadProcessId(IntPtr   hwnd,   out   int   ID);     
  protected   void   Button1_Click(object   sender,   EventArgs   e)     
  {     
      Excel.ApplicationClass   excel   =   new   Microsoft.Office.Interop.Excel.ApplicationClass();     
      excel.Workbooks.Open("d:\aaa.xls",   Type.Missing,   Type.Missing,   Type.Missing,   Type.Missing,   Type.Missing,   Type.Missing,   Type.Missing,   Type.Missing,   Type.Missing,   Type.Missing,   Type.Missing,   Type.Missing,   Type.Missing,   Type.Missing);     
      IntPtr   t   =   new   IntPtr(excel.Hwnd);     
      int   k   =   0;     
      GetWindowThreadProcessId(t,   out   k);     
      System.Diagnostics.Process   p   =   System.Diagnostics.Process.GetProcessById(k);     
      p.Kill();                     
   }  

同时需要修改配置文件machine.config

<processModel   enable="true"   timeout="Infinite"   idleTimeout="Infinite"   shutdownTimeout="0:00:05"   requestLimit="Infinite"   requestQueueLimit="5000"   restartQueueLimit="10"   memoryLimit="60"   webGarden="false"   cpuMask="0xffffffff"   userName="System"   password="AutoGenerate"   logLevel="Errors"   clientConnectedCheck="0:00:05"   comAuthenticationLevel="Connect"   comImpersonationLevel="Impersonate"   responseRestartDeadlockInterval="00:09:00"   responseDeadlockInterval="00:03:00"   maxWorkerThreads="25"   maxIoThreads="25"/>   
  userName="machine"   改为   userName="System"

以上部分在Win2008中找不到 没有设置成功

通过设置用户权限,Asp.net模拟用户等方式任然无法杀掉Excel进程,最后的解决办法是写一个自动杀Excel进程的服务。

using System;
using System.Diagnostics;
using System.ServiceProcess;

namespace KillExcel
{
    public partial class KillExcel : ServiceBase
    {
        public KillExcel()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            // TODO: 在此处添加代码以启动服务。
            double sleeptime =  60 * 1000; //1分钟
            System.Timers.Timer t = new System.Timers.Timer(sleeptime);//实例化Timer类,设置间隔时间为10000毫秒; 
            t.Elapsed += new System.Timers.ElapsedEventHandler(killExcel);//到达时间的时候执行事件; 
            t.AutoReset = true;//设置是执行一次(false)还是一直执行(true); 
            t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件; 

        }
        public void killExcel(object source, System.Timers.ElapsedEventArgs e)
        {
            Process[] myProcesses;
            DateTime startTime;
            myProcesses = Process.GetProcessesByName("Excel");
            //得不到Excel进程ID,暂时只能判断进程启动时间
            foreach (Process myProcess in myProcesses)
            {
                startTime = myProcess.StartTime;

                if ((DateTime.Now-startTime).Minutes>1)
                {
                    myProcess.Kill();
                }
            }
        }

        protected override void OnStop()
        {
        }
    }
}

 上边的服务出现不稳定问题,运行一段时间以后就会,自动杀死Excel进程,不再计算Excel启动时间与当前时间是否符合设定的时间差,还没找到原因。

计时器不能稳定运行,最后使用了线程方式来定时,这次可以稳定的运行了

public partial class KillExcel : ServiceBase
    {
        private int _timeOut;
        private Thread _t;
        public KillExcel()
        {
            InitializeComponent();
            int to;
            int.TryParse(ConfigurationManager.AppSettings["TimeOut"], out to);
            _timeOut = to > 0 ? to : 3;
        }

        protected override void OnStart(string[] args)
        {
            // TODO: 在此处添加代码以启动服务。
            _t = new Thread(new ThreadStart(Monitor));
            _t.IsBackground = true;
            _t.Start();
        }

        private void Monitor()
        {
            int sleeptime = 60 * 1000 * _timeOut;
            while (true)
            {
                Thread.Sleep(sleeptime);
                killExcel();
            }
        }

        public void killExcel()
        {
            Process[] myProcesses = Process.GetProcessesByName("Excel");
            //得不到Excel进程ID,暂时只能判断进程启动时间
            foreach (Process myProcess in myProcesses)
            {
                if ((DateTime.Now - myProcess.StartTime).Minutes > _timeOut)
                {
                    myProcess.Kill();
                }
            }
            GC.Collect();
        }

        protected override void OnStop()
        {
            
        }
    }