Excel is still running though I quit and released the object
-
Thursday, January 06, 2011 3:33 PM
In my application I am generating an Excel report and saving it in a location using C# codes. Once I saved I am closing the Excel and quiting the Excel application by code and I am releasing the memory of the EXCEL as well. However, what ever I do, the instance of the EXCEL is still showing in the Task Manager until I completely close the whole application
This is my code
//This is how I am Opening Excel file Microsoft.Office.Interop.Excel.Application xlApp; Microsoft.Office.Interop.Excel.Workbook xlWorkBook; Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet; xlApp = new Microsoft.Office.Interop.Excel.Application(); //after genrating the report this is how I am Closing and quiting xlWorkBook.Close(true, misValue, misValue); xlApp.Quit(); // Apart from I am releasing the Memory for Excel as follows releaseObject(xlWorkSheet); releaseObject(xlWorkBook); releaseObject(xlApp); //This is the method for releasing object private static void releaseObject(object obj) { try { System.Runtime.InteropServices.Marshal.ReleaseComObject(obj); obj = null; } catch (Exception ex) { obj = null; } finally { GC.Collect(); } }
Although I close, quit and released the object, why still EXCEL is in running? If user generates reports 5 times, 5 instance of the Excel is running (can see them in the Task Manager).
Please help me..
Dreaming a world without any war in anywhere- Moved by Leo Liu - MSFT Monday, January 10, 2011 8:36 AM Off-Topic, moved for better support. (From:Visual C# General)
All Replies
-
Thursday, January 06, 2011 4:14 PM
Hi Rajeeshun,
It's a simple question to ask yourself: does your computer get slow because of your problem. If it doesn't (at least this is what I would do) leave it as it is.
NVDPassie for CS
- Proposed As Answer by NVDPassie for CS Thursday, January 06, 2011 4:19 PM
-
Thursday, January 06, 2011 4:55 PM
Hi Rajeeshun,
It's a simple question to ask yourself: does your computer get slow because of your problem. If it doesn't (at least this is what I would do) leave it as it is.
NVDPassie for CS
Does not feel slowness in my computer as I have 3.00GHz Intel Core 2 Duo machine with 4GB RAM. However, users does not having good configuration and some of the users are in thin client (Citrix clients). Thefore performance will be an issue for them
Dreaming a world without any war in anywhere -
Thursday, January 06, 2011 5:07 PM
Hi Rajeeshun,
It's a simple question to ask yourself: does your computer get slow because of your problem. If it doesn't (at least this is what I would do) leave it as it is.
NVDPassie for CS
This is terrible advice.
Rajeeshun, in your code (as a temporary measure) make Excel visible. Something in Excel might be waiting for a user input. Making Excel visible might give clues as to why Excel isn't quitting.
…we each have more potential than we might ever presume to guess. (Blog: http://dsmyth.blogspot.com/) -
Thursday, January 06, 2011 5:18 PMHi
You need to call the Quit-method of the excel application object:
xlApp.Quit();
which will also release related objects such as workbook, worksheets.
Calling the realeaseObject stuff won't do harm, but is optional afaics.
Afaics all top-level Office-Automation objects have a Quit method
you need to call for disposing
Chris
-
Thursday, January 06, 2011 6:34 PM
Hi
You need to call the Quit-method of the excel application object:
xlApp.Quit();
which will also release related objects such as workbook, worksheets.
Calling the realeaseObject stuff won't do harm, but is optional afaics.
Afaics all top-level Office-Automation objects have a Quit method
you need to call for disposing
Chris
I have that (xlApp.Quit();) in my code and still having same problemAs like Dereck suggested, now I am opeing the Excel file for user to view the report and save their prefered location
Dreaming a world without any war in anywhere -
Thursday, January 06, 2011 7:19 PMThis is so typical of running Excel in this manner. The only solution I found was to kill the orphaned processes, after releasing everything and closing Excel. It's enough to drive you insane. =P
-
Thursday, January 06, 2011 10:43 PM
Stupid me, you had xlApp.Quit() in the code you've posted :-)
Must have overseen it.
I experienced problems with closing excel through code when I had unsaved data
or a workbook couldn't be saved to an existing file location, cause it was in use
or another file-accessibility issue.Usually a popup flashed up then, even with xlApp.Visible = false, but sometimes it simply
got hung.Testing it out in visible mode and see if there is still something to be finished, is likely
a good idea.
Chris -
Friday, January 07, 2011 1:29 PM
Hi
You need to call the Quit-method of the excel application object:
xlApp.Quit();
which will also release related objects such as workbook, worksheets.
Calling the realeaseObject stuff won't do harm, but is optional afaics.
Afaics all top-level Office-Automation objects have a Quit method
you need to call for disposing
Chris
I have that (xlApp.Quit();) in my code and still having same problemAs like Dereck suggested, now I am opeing the Excel file for user to view the report and save their prefered location
Dreaming a world without any war in anywhereIf you still want Excel to be hidden then you can save the worksheet for them through Excel. Although I have not ran your code I believe what was happening was Excel was open and hidden a worksheet was created and it's values set, then a call to Quit() was called. However there was unsaved changes in the workbook so a user prompt is shown but since Excel was hidden the prompt can't be seen and can't be clicked... Excel process remained open.
If Excel should remain hidden and should close correctly then the workbooks unsaved changes need to be saved or ignored; and there are Excel commands to do those. Just call them from your code before calling Quit.
…we each have more potential than we might ever presume to guess. (Blog: http://dsmyth.blogspot.com/) -
Friday, January 07, 2011 3:10 PM
Hi
You need to call the Quit-method of the excel application object:
xlApp.Quit();
which will also release related objects such as workbook, worksheets.
Calling the realeaseObject stuff won't do harm, but is optional afaics.
Afaics all top-level Office-Automation objects have a Quit method
you need to call for disposing
Chris
I have that (xlApp.Quit();) in my code and still having same problemAs like Dereck suggested, now I am opeing the Excel file for user to view the report and save their prefered location
Dreaming a world without any war in anywhereIf you still want Excel to be hidden then you can save the worksheet for them through Excel. Although I have not ran your code I believe what was happening was Excel was open and hidden a worksheet was created and it's values set, then a call to Quit() was called. However there was unsaved changes in the workbook so a user prompt is shown but since Excel was hidden the prompt can't be seen and can't be clicked... Excel process remained open.
If Excel should remain hidden and should close correctly then the workbooks unsaved changes need to be saved or ignored; and there are Excel commands to do those. Just call them from your code before calling Quit.
…we each have more potential than we might ever presume to guess. (Blog: http://dsmyth.blogspot.com/)
Actually, I am calling xlApp.Quit(), after saving the file in local directorythis is the code for that
xlWorkBook.SaveAs(Excel_File_Details, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue,false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); xlWorkBook.Close(true, misValue, misValue); xlApp.Quit(); releaseObject(xlWorkSheet); releaseObject(xlWorkBook); releaseObject(xlApp); //MessageBox.Show("Payment Info Report Saved in C:\\Payment_Info folder"); //open the file for user System.Diagnostics.Process proc = new System.Diagnostics.Process(); proc.EnableRaisingEvents = false; proc.StartInfo.FileName = "C:\\Payment_Info\\Payment_Info.xls"; proc.Start();
Earlier, I was saving the file in local directory and then prompting a message to tell the saved location..Now I changed that to 1.Saving the file in the local directory, 2. then opening the file for user. Now user has to close the file and that removes the instance of the Excel file
Anyone know, how to check the file open status? I need to check the file is already opened or not before I save the file? Any clue
Dreaming a world without any war in anywhere -
Wednesday, February 23, 2011 4:40 PM
Just for a update
I am writing an automation application that does many formatting jobs on worksheets (rearaanging columns, fillind data, adding rows/cols etc)....Since there are many tasks involves on Worksheet, I got same "Excel is not closing" issue though I forcibly called garbage collection (ReleaseObject).. Finally I chose the unfair method to kill the Excel instance in Task Manager with following method.
I dont see any other good way to remove the Excel instance or Close the Excel other than this :(
private void killExcel()
{
System.Diagnostics.Process[] PROC = System.Diagnostics.Process.GetProcessesByName("EXCEL");
foreach (System.Diagnostics.Process PK in PROC)
{
if (PK.MainWindowTitle.Length == 0)
{
PK.Kill();
}
}
}Hope this helps someone who desperately need some working solution like me
Dreaming a world without any war in anywhere- Marked As Answer by Rajeeshun Wednesday, February 23, 2011 4:40 PM
-
Wednesday, June 27, 2012 12:09 AM
I had this problem too.
1. You need to call Close on your Workbooks collection as well as your Workbook.
2. Put a while loop around your Marshal.ReleaseComObject calls (and good to have this code in a Try block).
while (Marshal.ReleaseComObject(object_name) > 0) ;
MAKE SURE THAT THE SEMI-COLON IS THERE OR YOU WILL BE LOOPING YOUR NEXT LINE.
3. Close, Release COM Object, and set to null all the Interop objects you explicitly create, in the reverse order they were created. Like pushing to and popping from a stack, the pops are in the reverse order of the pushes. In my case, the order is:
ExcelSheet (Release COM object, then set to null),
ExcelWorkbook (call Close, then Release COM Object, then set to null),
ExcelWorkbooksCollection (call Close, then Release COM Object, then set to null),
ExcelApplication (call Quit, then Release COM Object, then set to null).
4. Force garbage collection after ExcelApplication is released and set to null.
GC.Collect();
GC.WaitForPendingFinalizers();
5. Make sure your Close and Quit calls are final, and won't prompt for a save file name or something like that. Your ExcelWorkbook.Close() call should specify false to not save, or true with a given file path.
(Show the Excel application (Visible property = true) after you create the application object, to actually SEE the application, and figure out where it's being held up, if you're still having trouble.)
- Edited by FreeSlave Wednesday, June 27, 2012 12:38 AM wrong identifier

