tags:

views:

126

answers:

5

Actually i m writing a small application(Win Form + C#), where i am reading the Excel file. After exiting from the application and went to the task manager i found that Excel.exe is still running. And as i run my application several times i found multiple instances of Excel.exe running in the task manager.

So can somebody tell me as what needs to be done to kill "Excel.exe" each time i exit from the application...

The code is somewhat like this:

ApplicationClass appClass = new ApplicationClass();
Workbook workBook = appClass.Workbooks.Open(path,0,true,5,"","",true,XlPlatform.xlWindows,"\t",false,false,0,true,1,0);
Worksheet workSheet = (Worksheet)workBook.ActiveSheet;

All under this namespace: Microsoft.Office.Interop.Excel; that is COM

+2  A: 

Check out this example - http://www.dreamincode.net/code/snippet1543.htm

It uses the Process.Kill() method - http://msdn.microsoft.com/en-us/library/system.diagnostics.process.kill.aspx

Kris Krause
This will not work as its quite possible that other application might be working using Excel.exe. So this will kill all the instances of the "Excel" process.
Amit
+2  A: 

This answer assumes that you are launching Excel via Process.Start. The code wasn't posted until later.

In that case keep hold of the process handle returned by that call and then use that to call Process.CloseMainWindow followed by Process.Close:

Process ExcelProcess;  // Declaration

Store the process handle:

ExcelProcess = Process.Start(...);

Then when you exit your application:

// Close process by sending a close message to its main window.
ExcelProcess.CloseMainWindow();
// Free resources associated with process.
ExcelProcess.Close();
ChrisF
Btw, `Process.Close` doesn't exit a process; it just cleans up your app's copy of the process handle. Failing to call `Process.Close` doesn't prevent the process from exiting.
Tim Robinson
ChrisF
I am using Microsoft.Office.Interop.Excel.ApplicationClass. The code is somewhat like this: //Initializing the application class ApplicationClass appClass = new ApplicationClass(); Workbook workBook = appClass.Workbooks.Open(path,0,true,5,"","",true,XlPlatform.xlWindows,"\t",false,false,0,true,1,0); Worksheet workSheet = (Worksheet)workBook.ActiveSheet;
Amit
@Amit - update your question with this code - it'll be much easier to read.
ChrisF
+5  A: 

Instead of killing the processes... why don't you dispose your Excel-handles correctly instead? Sounds like you open instances of Excel without ever closing them properly. It's generally a good idea to also release acquired resources after using them.

Christian
I tried using Marshal.ReleaseComObject(handler). But its not working :(
Amit
As far as I can tell by your comments to ChrisF's answer, you're using the com interface via interop. In this case call the Quit-Method of the instance (as Tim Robinson pointed out)
Christian
+8  A: 

How are you reading the Excel file? If you're using COM, you need to call Application.Quit when you're finished.

Edit: from the code in your comment -

//Initializing the application class
ApplicationClass appClass = new ApplicationClass();
try
{
    Workbook workBook = appClass.Workbooks.Open(path,0,true,5,"","",true,XlPlatform.xlWindows,"\t",false,false,0,true,1,0);
    Worksheet workSheet = (Worksheet)workBook.ActiveSheet;
    // do stuff with the workbook
}
finally
{
    appClass.Quit();
}
Tim Robinson
I am using Microsoft.Office.Interop.Excel.ApplicationClass, yes COM.Actually i am getting the information from the Excel and showing it the user in Datagrid view. Now its upto the user to close that application using "Close" button.I hope you got a hint now as what i am doing, any suggestions now ?
Amit
Yeah its working :) Thanks Tim.
Amit
As an aside, you should not use ApplicationClass. Quote the documentation: "This class supports the .NET Framework infrastructure and is not intended to be used directly from your code.", use Application instead.
Mathias
A: 
Process[] pro = Process.GetProcessesByName("excel");

            pro[0].Kill();
            pro[0].WaitForExit();

its easy like this. you can get process by name, id etc. just go to your process, Kill(), and WaitForExit();

Serkan Hekimoglu