2

I use some objects like workbook, worksheet, app for Excel tasks. After I am done with Excel, I try releasing them by the code below. After all, I still see EXCEL.EXE at Task Manager. Why doesn't it release completely?

My class for Excel tasks:

Excel._Application app = new Excel.Application();
Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
Excel._Worksheet worksheet = null;
app.Visible = true;

worksheet.Cells[1, 1] = "test string";

workbook.SaveAs("C:\testfile.xlsx");

object misValue = System.Reflection.Missing.Value;
workbook.Close(true, misValue, misValue);
app.Quit();

releaseObject(worksheet2);
releaseObject(workbook);
releaseObject(app);

releaseObject class:

    private void releaseObject(object obj)
    {
        try
        {
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
            obj = null;
        }
        catch (Exception ex)
        {
            obj = null;
            MessageBox.Show("Unable to release the Object " + ex.ToString());
        }
        finally
        {
            GC.Collect();
        }
    } 

EXCEL.EXE is still at Task Manager: enter image description here

Ned
  • 1,055
  • 9
  • 34
  • 58
  • 1
    You might try also setting `app`, `workbook`, and `worksheet` to `null` to indicate to the GC that references to these objects are no longer needed. – Brad Rem Aug 05 '12 at 23:04
  • 1
    `Excel._Worksheet worksheet = null;` and subsequent uses of an object called `worksheet` do not match up to `releaseObject(worksheet2);` but that may just be a typo here rather than being in the original code – barrowc Aug 05 '12 at 23:26
  • @barrowc has hit the nail on the head. I doubt it is a typo. I use the same `releaseObject(object obj)` and never had a problem.... – Siddharth Rout Aug 06 '12 at 00:15
  • worksheet2 is a typo as you guess. Assigning null didn't work. There are 3 good idea in this question page but none of them worked. This problem really annoys me. If I close the application, all EXCEL.EXE files are terminated. I don't close it, all EXCEL.EXE files stay at background! – Ned Aug 06 '12 at 02:38
  • did you get a solution to this irritating issue? – Alex Gordon Aug 13 '12 at 23:19

5 Answers5

4

Already answered see:

How do I properly clean up Excel interop objects?

Basically COM objects are unmanaged objects (duh) and are not released when they go out of scope. System.Runtime.InteropServices.Marshal.ReleaseComObject() can be used to release them before they get finalized (in the garbage collector), but you have to make sure to keep a reference of all the objects: excelApp.Worksheets.Open() creates two objects: the collection 'Worksheets' and the 'Worksheet' opened with the Open() method.

Community
  • 1
  • 1
Eli Algranti
  • 8,707
  • 2
  • 42
  • 50
2

It's very easy to kill the EXCEL object from the Task Manager using Process, see below & enjoy..

//Kill the all EXCEL obj from the Task Manager(Process)
System.Diagnostics.Process[] objProcess = System.Diagnostics.Process.GetProcessesByName("EXCEL");

if (objProcess.Length > 0)
{
    System.Collections.Hashtable objHashtable = new System.Collections.Hashtable();

    // check to kill the right process
    foreach (System.Diagnostics.Process processInExcel in objProcess)
    {
        if (objHashtable.ContainsKey(processInExcel.Id) == false)
        {
             processInExcel.Kill();
        }
    }
    objProcess = null;
}

//In case of you want to quit what you have created the Excel object from your application //just use below condition in above,

    if(processInExcel.MainWindowTitle.ToString()== "")
1

It's been a while since I've done Office Interop (I actually was using it for Outlook, but the concepts should be the same). The only way I was able to completely release everything was to do this:

GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();

Give it a shot....

BFree
  • 102,548
  • 21
  • 159
  • 201
  • +1 I was waiting for this answer, but try this on for size http://stackoverflow.com/questions/3829928/under-what-circumstances-we-need-to-call-gc-collect-twice – Jeremy Thompson Aug 06 '12 at 01:40
  • It seems good idea but didn't work, unfortunately – Ned Aug 06 '12 at 02:35
1

this is a problem that i could not solve. ive seen all of these answers before and i've implemented all of them, and still excel did not close. i had to do things with brute force. what i did was created a BAT file and ran it from my program. here are the contents of the file:

Taskkill /F /IM excel.exe

this will call ALL of your excels running. i would use this if you cannot figure out a different solution.

Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062
0

You need to close the excel application by doing like this as it opens excel program in background....

app .Application.Quit();

Add the above line code in your code in following way:

    object misValue = System.Reflection.Missing.Value;
    workbook.Close(true, misValue, misValue);
    app .Application.Quit();
    app.Quit();
Akash KC
  • 16,057
  • 6
  • 39
  • 59