[转]Excel关闭进程

 原文地址:http://www.cnblogs.com/feishu/archive/2010/05/08/1730797.html

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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进程的服务。

 

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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启动时间与当前时间是否符合设定的时间差,还没找到原因。

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

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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()
         {
             
         }
     }

转载于:https://www.cnblogs.com/lisengl/p/3317272.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值