Killing interaction process

I have the following program (written in VB.NET):

Imports Microsoft.Office.Interop.Excel Public Class Form1 Dim eApp As New Excel.Application Dim w As Excel.Workbook w = eApp.Workbooks.Open( "path.xls", ReadOnly:=True) .. Processing Code .. //Attempts at killing the excel application w.Close() eApp.Workbooks.Close() eApp.Quit() End Class 

When I run this several times, I get a bunch of EXCEL.EXE instances in my task manager. How can I kill these processes in code? All of the code is verified and does not work.

+4
source share
9 answers

I should have done this a while ago in NET 1.1, so please forgive the rust.

Hwind appeared on eApp (win32 window handle - http://msdn.microsoft.com/en-us/library/bb255823.aspx ) or a similar object. I had to use this and pInvoke ( http://www.pinvoke.net/default.aspx/user32.GetWindowThreadProcessId ) to get the process id. With this, I was able to do Process.Kill () on exe.

There may be a better way to do it now, but it should work.

+3
source

encode the next line of code until the result is zero or less

 System.Runtime.InteropServices.Marshal.ReleaseComObject(eApp) 

or look at the link

+3
source

To exit Excel, you must call Application.Quit and use one of the following methods.

  • Call Marshal.ReleaseComObject on each instance of the Excel object. There's a KnowledgeBase article that describes how to do this. For example, in your sample code, "eApp.Workbooks.Open" creates an instance of the Workbooks object without assigning it a variable. You need to assign a variable as described in the KB article so that you can release it later. The trick is that with nothing but the simplest automation scripts, it’s very difficult to make sure that you always issue all such objects in all code paths (for example, using try / finally to ensure that they will be thrown when an exception is thrown )

  • Call GC.Collect and GC.WaitForPendingFinalizers after releasing the last Excel object. Some people have suggested that you may need to do this twice.

+2
source

Launch Excel:

 Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); Workbook wb = xlApp.Workbooks.Open(...); Worksheet ws = (Worksheet)wb.Worksheets[1]; 

Kill Excel:

 System.Runtime.InteropServices.Marshal.ReleaseComObject(ws); System.Runtime.InteropServices.Marshal.ReleaseComObject(wb); wb = null; ws = null; uint idProcess; GetWindowThreadProcessId((IntPtr)xlApp.Hwnd, out idProcess); xlApp.DisplayAlerts = false; xlApp.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp); xlApp = null; xlApp = null; GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); Process processo = null; try { processo = Process.GetProcessById((int)idProcess); } catch { } if (processo != null) processo.Kill(); 
+1
source

I posted a solution for this a few days ago: Killing Excel.EXE on the server

The same method as StingyJack; the only thing i know to really work.

0
source

Use block

 Using eApp As New Excel.Application Using w As Excel.Workbook w = eApp.Workbooks.Open( "path.xls", ReadOnly:=True) .. Processing Code .. //Attempts at killing the excel application w.Close() eApp.Workbooks.Close() eApp.Quit() End Using End Using 

Like try / finally with deletion at the end, this will automatically remove Excel.Application and Excel.Workbook.

As mentioned in another post, you may also need System.Runtime.InteropServices.Marshal.ReleaseComObject (eApp) ????

0
source

After ExcelApplication.Quit (), add the following code.

It will destroy the excel application from the process.

  Process[] Proc = Process.GetProcessesByName("Excel"); foreach (Process p in Proc) { if (p.MainWindowTitle == "") p.Kill(); } 
0
source

Tagore Peethala's solution is straight forward and works. Basically make sure that WorkBook.CLOSE .. then ExcelApplication.QUIT ... then just find an excel instance that works without anything open and closes it.

 myExcelWorksheet = null; if (myExcelWorkbook != null) { myExcelWorkbook.Close(); myExcelWorkbook = null; } if (myExcelApp != null) { myExcelApp.Quit(); myExcelApp = null; } foreach (System.Diagnostics.Process myProcess in System.Diagnostics.Process.GetProcessesByName("Excel")) { if (myProcess.MainWindowTitle == "") { myProcess.Kill(); } } 
0
source
 //Quit Excel application eApp.Quit(); //Release COM objects (every object you used in eApp,like workbooks, Workbook, Sheets, Worksheet) System.Runtime.InteropServices.Marshal.ReleaseComObject(obj); obj = null; // Force garbage collector cleaning GC.Collect(); 
0
source

All Articles