Share via


excel stays live as a process when terminating a program during debug

Question

Thursday, December 25, 2014 4:56 PM

Hi

In my C# windows application I open an excel file with

Excel.Application xlApp = new Excel.ApplicationClass();

when in debug mode in VS I often stop the application mid process by clicking on "stop debugging" in the VS menu. I noticed that this seems to leave many opened
EXCEL.EXE *32 processes (I see them in task manager)

This eventually clogs my computer.

I assume it is because the application never reached the

xlApp.Quit();

which I have when the application is in ordinary mode.

What can I do about it so that I would not have all these processes staying alive after I stopped the program midway in debug?

All replies (7)

Thursday, December 25, 2014 8:04 PM âś…Answered

Hello,

The short answer is too keep Task Manager open to the process tab and kill Excel when you stop prior to a normal termination of the project in debug mode.

The proper method to shut Excel down is shown below. Now if you are not doing it this way most likely Excel is being disposed of when closing the form which has the Excel code in it or when the initial form closes.

Very simple example to dispose of objects ASAP 

using Excel = Microsoft.Office.Interop.Excel;

.

private void button1_Click(object sender, EventArgs e)
{
    OpenExcelExample("SomeExcelFile.xlsx", "Sheet1");
}

.

public void OpenExcelExample(string FileName, string SheetName)
{
    if (System.IO.File.Exists(FileName))
    {
        bool Proceed = false;
        Excel.Application xlApp = null;
        Excel.Workbooks xlWorkBooks = null;
        Excel.Workbook xlWorkBook = null;
        Excel.Worksheet xlWorkSheet = null;
        Excel.Sheets xlWorkSheets = null;
        Excel.Range xlCells = null;
        xlApp = new Excel.Application();
        xlApp.DisplayAlerts = false;
        xlWorkBooks = xlApp.Workbooks;
        xlWorkBook = xlWorkBooks.Open(FileName);
        xlApp.Visible = false;
        xlWorkSheets = xlWorkBook.Sheets;
        //
        // For/Next finds our sheet
        //
        for (int x = 1; x <= xlWorkSheets.Count; x++)
        {
            xlWorkSheet = (Excel.Worksheet)xlWorkSheets.get_Item(x);

            if (xlWorkSheet.Name == SheetName)
            {
                Proceed = true;
                break;
            }
            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlWorkSheet);
            xlWorkSheet = null;
        }

        if (Proceed)
        {
            Excel.Range xlRange1 = null;
            xlRange1 = xlWorkSheet.get_Range("A1");
            xlRange1.Value = "Hello";
            Marshal.FinalReleaseComObject(xlRange1);
            xlRange1 = null;
            xlWorkSheet.SaveAs(FileName);
        }
        else
        {
            MessageBox.Show(SheetName + " not found.");
        }
        xlWorkBook.Close();
        xlApp.UserControl = true;
        xlApp.Quit();
        ReleaseComObject(xlCells);
        ReleaseComObject(xlWorkSheets);
        ReleaseComObject(xlWorkSheet);
        ReleaseComObject(xlWorkBook);
        ReleaseComObject(xlWorkBooks);
        ReleaseComObject(xlApp);
        MessageBox.Show("Done");
    }
    else
    {
        MessageBox.Show("'" + FileName + "' not located. Try one of the write examples first.");
    }
}

.

private void ReleaseComObject(object obj)
{
    try
    {
        if (obj != null)
        {
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
            obj = null;                    
        }
    }
    catch
    {
        obj = null;
    }
}

If you don't have other instances open outside of Visual Studio you could write a utility to shut down all instances of Excel but why when you can do this in Task Manager process tab.

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.


Friday, December 26, 2014 5:24 AM

You need to Close it explicitly. This post will help you.

http://stackoverflow.com/questions/17777545/closing-excel-application-process-in-c-sharp-after-data-access

chanmm

chanmm


Tuesday, December 30, 2014 2:57 PM

The problem occurs when the program is cut short in the middle of debug before it reaches the part of Quit, Close, ReleaseComObject etc...

I do have other opened excel files unrelated to the app and i don't know how to identify the processes that need to be shut down when the debug session has terminated


Tuesday, December 30, 2014 2:57 PM

The problem occurs when the program is cut short in the middle of debug before it reaches the part of Quit, Close, ReleaseComObject etc...

I do have other opened excel files unrelated to the app and i don't know how to identify the processes that need to be shut down when the debug session has terminated


Tuesday, December 30, 2014 4:00 PM

You're talking to Excel via COM so if the COM object is not properly closed then it'll hang around until it is eventually unloaded by the OS.  In general you should avoid stopping the debugger when a resource like this is open. If that isn't possible then the alternative is to use the Immediate window and explicitly invoke the method in question to close the object (assuming it is in scope) before terminating the app.  It is a hack but so is stopping the debugger mid stream.

Michael Taylor
http://blogs.msmvps.com/p3net


Tuesday, January 6, 2015 2:18 PM

how long should it ususally take before windows eventually closes it on its own? Is there a way to explicitly tell windows to do this type of garbage collection NOW?


Tuesday, January 6, 2015 2:33 PM

Depending on the objects used it might take 10 minutes, perhaps longer or not until you kill it in Task Manager.

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.