0

I have a c# application that creates a simple excel file. This work fine. I however want to make sure the excel application that is created is closed no matter what happens so I don't get lots of Excel processes running in the background.

Below is some code I'm using. I guess my question is in the Dispose method is there a way to test if the XlApp is still open and if so make sure the application is quitted.

Is it just as simple as the few lines below?

 if (XlApp != null)
 {
     XlApp.Quit();
     releaseObject(XlApp);
 }

My code example

public class ExcelMain : IDisposable
{
        public void MadeUpMethod()
        {
            // do some stuff here
            XlApp.Quit();     // worried that if the line is not reached will the excel application be closed
        }

        public void Dispose()
        {
            Dispose(true);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (Rng != null) releaseObject(Rng);
                if (XlWorksheet != null) releaseObject(XlWorksheet);
                if (XlSheets != null) releaseObject(XlSheets);
                if (XlWorksheets != null) releaseObject(XlWorksheets);
                if (XlWorkbook != null) releaseObject(XlWorkbook);
                if (ActiveWindow != null) releaseObject(ActiveWindow);
                if (XlApp != null) releaseObject(XlApp);
            }
        }

        private static void releaseObject(object obj)
        {
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
        }
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
mHelpMe
  • 6,336
  • 24
  • 75
  • 150
  • I don't know if this is an option for you, but I suggest avoiding Office interop and using [a C# Excel library](http://stackoverflow.com/q/151005/87698) instead. (Personally, I use [SpreadsheetLight](http://spreadsheetlight.com/), but the others are fine as well.) – Heinzi Sep 08 '15 at 07:36
  • This question has been asked many times before. For example: http://stackoverflow.com/questions/158706/how-to-properly-clean-up-excel-interop-objects – varocarbas Sep 08 '15 at 07:41
  • Thanks for the link. I have seen that post before. What I'm asking above though is I think slightly different. I want to know how to test if the excel application is still open and if so then call the quit function – mHelpMe Sep 08 '15 at 07:44
  • 1
    @Heinzi I am not sure that recommending a random library without giving any kind of reason for that is precisely on-topic (and/or helping the asker). I personally like Interop and haven't found any kind of problem with it; but if someone prefers to use a different alternative, why proposing another thing? IMO, Excel interop is good for desktop-based development (have heard about problems with ASP; although not completely sure either); also it is the most widely (and best documented, discussed-in-SO, etc.) alternative. – varocarbas Sep 08 '15 at 07:44
  • 1
    The provided link(s) are quite descriptive on this front: understand them properly and make sure that you implement all what is required to release the used objects when closing the application; if your code is right, no Excel process would continue running. The other alternative (not recommendable at all) would be looking at the running processes, locating the ones you want (i.e., Excel ones) and killing them. But as said doing such a thing is not required if you release the objects properly (as explained in quite a few other SO posts). – varocarbas Sep 08 '15 at 07:49

0 Answers0