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

5
  • 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. Commented Aug 5, 2012 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 Commented Aug 5, 2012 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.... Commented Aug 6, 2012 at 0: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! Commented Aug 6, 2012 at 2:38
  • did you get a solution to this irritating issue? Commented Aug 13, 2012 at 23:19

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

Comments

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()== "")

Comments

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....

2 Comments

+1 I was waiting for this answer, but try this on for size stackoverflow.com/questions/3829928/…
It seems good idea but didn't work, unfortunately
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.

Comments

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();

1 Comment

didn't work :( I have tried it a few times

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.