在Windows服务中从C#中杀死EXCEL.exe进程 [英] Killing EXCEL.exe Process from C# in a Windows Service

查看:203
本文介绍了在Windows服务中从C#中杀死EXCEL.exe进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows服务,通过Microsoft.Office.Interop.Excel.Application对象打开Excel电子表格。

 工作簿工作簿= xlApp.Workbooks.Open(fileName,2,false); 
...
...
workbook.Close();
xlApp.Quit();

我想杀死在完成工作后剩下的EXCEL.exe进程工作簿



我尝试过以下操作,没有成功...

  //返回一个processId为0 
IntPtr processId;
GetWindowThreadProcessId(new IntPtr(xlApp.Hwnd),out processId);
进程p = Process.GetProcessById(processId.ToInt32());
p.Kill();任何人有什么想法可以通过Windows服务来做到这一点?

$ / $ $ $ $ $ $ $ $ $

解决方案

经过很多阅读和挫败,我找到了一个解决方案!



所有信用额度均来自 dotNetkow nightcoder Mike Rosenblum for他们在这篇文章上的解决方案:如何正确清理Excel互操作对象?



这是我做的...


1.更改了项目释放(在DEBUG模式下,COM对象很难处理其引用


2.删除所有双点表达式(所有COM对象都应绑定到一个变量所以他们可以被释放)


3.在finally块中明确地调用GC.Collect(),GC.WaitForPendingFinalizers()和Marshal.FinalReleaseComObject()



这是我使用的acutal代码:

 申请ation xlApp = null; 
工作簿工作簿= null;
工作簿工作簿= null;
工作表sheet = null;
范围r = null;
object obj = null;

try
{
xlApp = new Application();
xlApp.DisplayAlerts = false;
xlApp.AskToUpdateLinks = false;
workbooks = xlApp.Workbooks;
workbook = workbooks.Open(fileName,2,false);
sheet = workbook.Worksheets [1];

r = sheet.get_Range(F19);
obj = r.get_Value(XlRangeValueDataType.xlRangeValueDefault);
}
finally
{
GC.Collect();
GC.WaitForPendingFinalizers();
if(value!= null)Marshal.FinalReleaseComObject(value);
if(r!= null)Marshal.FinalReleaseComObject(r);
if(sheet!= null)Marshal.FinalReleaseComObject(sheet);
if(workbooks!= null)Marshal.FinalReleaseComObject(workbooks);
if(workbook!= null)
{
workbook.Close(Type.Missing,Type.Missing,Type.Missing);
Marshal.FinalReleaseComObject(workbook);
}
if(xlApp!= null)
{
xlApp.Quit();
Marshal.FinalReleaseComObject(xlApp);
}
}


I have a windows service that opens up an Excel spreadsheet via the Microsoft.Office.Interop.Excel.Application object.

Application xlApp = new Application();
Workbook workbook = xlApp.Workbooks.Open(fileName, 2, false);
...
...
workbook.Close();
xlApp.Quit();

I would like to kill the EXCEL.exe process that is left running after it is done working with the workbook.

I've tried the following with no success...

// This returns a processId of 0
IntPtr processId;
GetWindowThreadProcessId(new IntPtr(xlApp.Hwnd), out processId);
Process p = Process.GetProcessById(processId.ToInt32());   
p.Kill();

Anyone have any ideas as to how I can do this via a Windows Service?

解决方案

After much reading and frustration I've found a solution!

All credit goes to dotNetkow, nightcoder and Mike Rosenblum for their solutions on this post: How do I properly clean up Excel interop objects?

Here is what I did...
1. Changed build mode of the project to "Release" (in DEBUG mode, COM objects have a hard time disposing of their references.
2. Removed all double dot expressions (all COM objects should be tied to a variable so they can be released)
3. Calling GC.Collect(), GC.WaitForPendingFinalizers(), and Marshal.FinalReleaseComObject() explicitly in a finally block

Here is the acutal code I am using:

Application xlApp = null;
Workbooks workbooks = null;
Workbook workbook = null;
Worksheet sheet = null;
Range r = null;
object obj = null;

try
{
    xlApp = new Application();
    xlApp.DisplayAlerts = false;
    xlApp.AskToUpdateLinks = false;
    workbooks = xlApp.Workbooks;
    workbook = workbooks.Open(fileName, 2, false);
    sheet = workbook.Worksheets[1];

    r = sheet.get_Range("F19");
    obj = r.get_Value(XlRangeValueDataType.xlRangeValueDefault);
}
finally
{
    GC.Collect();
    GC.WaitForPendingFinalizers();
    if (value != null) Marshal.FinalReleaseComObject(value);
    if (r != null) Marshal.FinalReleaseComObject(r);
    if (sheet != null) Marshal.FinalReleaseComObject(sheet);
    if (workbooks != null) Marshal.FinalReleaseComObject(workbooks);
    if (workbook != null)
    {
        workbook.Close(Type.Missing, Type.Missing, Type.Missing);
        Marshal.FinalReleaseComObject(workbook);
    }
    if (xlApp != null)
    {
        xlApp.Quit();
        Marshal.FinalReleaseComObject(xlApp);
    }
}

这篇关于在Windows服务中从C#中杀死EXCEL.exe进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆