We do not always find visible happiness in proportion to visible virtue

梦幻白桦林

SHARE

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  14 Posts :: 58 Stories :: 62 Comments :: 0 Trackbacks

公告

常用链接

留言簿(5)

搜索

  •  

最新随笔

最新评论

阅读排行榜

关闭进程

C#和Asp.net下excel进程一被打开,有时就无法关闭,   尤其是website.对关闭该进程有过GC、release等方法,但这些方法并不是在所有情况下均适用。  于是提出了kill   process的方法,   目前我见过的方法多是用进程创建时间筛选excel.exe进程,   然后kill 。     这样的方法是不精确的,   也是不安全的,   通过对网上一些关于Api运用文章的阅读,   我找到了更为直接精确找到这个process并kill的方法,以下就是代码        
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();                  
   }


以上代码百分百成功的关闭excel.exe进程
我的做法是结合两者,先释放资源,然后关闭进程。
同时网上说避免使用GC.Collect 方法 (),因为会导致整个clr进行gc,影响你的性能.所以我也没有调用GC.Collect

posted on 2007-06-01 11:51 colys 阅读(5260) 评论(9)  编辑 收藏 引用 所属分类: C#.Net

Feedback

# re: c#操作excel后关闭excel.exe的方法 2007-07-02 11:21 Jet
k 的值怎么一直都是0啊.  回复  更多评论
  

# re: c#操作excel后关闭excel.exe的方法 2007-07-02 11:44 Jet
补充一句:我应用在WEB方面  回复  更多评论
  

# re: c#操作excel后关闭excel.exe的方法 2007-07-02 12:23 colys
你的excel.Hwnd有值吗?会不会是前面已经释放掉了  回复  更多评论
  

# re: c#操作excel后关闭excel.exe的方法 2007-07-02 14:02 Jet
是有值的,是197048
没有释放掉  回复  更多评论
  

# re: c#操作excel后关闭excel.exe的方法 2007-07-02 14:26 colys
具体的我也不清楚为什么,我只能把我可以用的代码贴给你:
Application ExApp;
Workbook Xbook;
Worksheet XSheet;

/// <summary>
/// winapi 用于找到句柄线程
/// </summary>
/// <param name="hwnd"></param>
/// <param name="ID"></param>
/// <returns></returns>
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID)

//关闭和销毁
public override void Close()
{
ExApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(XSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(Xbook);
//调用window api查找Excel进程,并用关闭
IntPtr t = new IntPtr(ExApp.Hwnd);
int ProcessById;
GetWindowThreadProcessId(t, out ProcessById);
System.Diagnostics.Process ExcelProcess = System.Diagnostics.Process.GetProcessById(ProcessById);
ExcelProcess.Kill();
XSheet = null;
Xbook = null;
ExApp = null;
}
  回复  更多评论
  

# re: c#操作excel后关闭excel.exe的方法 2007-07-02 15:04 Jet
ProcessById为0 我觉得应该是应用环境问题 我是应用于WEB.  回复  更多评论
  

# re: c#操作excel后关闭excel.exe的方法 2008-08-26 19:52 tsing
@Jet
Jet 你好你的问题解决了吗? 我也遇到了同样的问题。在开发机器上可以,服务器上就不能,得到拒绝访问的错误信息

高手们指点一下,我该怎么解决!  回复  更多评论
  

# re: c#操作excel后关闭excel.exe的方法 2009-02-09 10:13 meteor85
直接app.quit();就可以了
  回复  更多评论
  

# re: c#操作excel后关闭excel.exe的方法 2009-04-16 11:23 laomattkl
在本机调试运行可以,放到服务器上就拒绝访问了,为什么呀?  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理