tags:

views:

102

answers:

1

Basically I have a program written in C# which uses excel application.

While running my program, if the user tries to open any excel file, that file is opened on the excel instance created by my program.

I suppose this is related to the fact that excel opens on to existing instance of " Excel.exe ".

It causes zombie excel process when my program exits.

What I'd like to do is hide my excel instance from being used by user. (Or any other solution for the situation)

I tried to google something out but nothing so far.

My code looks like this.

foreach (string strFileName in this.m_ListRequestFileNames)
{
    object misVal = System.Reflection.Missing.Value;
    try
    {
        if (Common.IsOpened(strFileName) == true)
        {
            this.m_ParentWnd.Log(strFileName + " is in use by another program." + Environment.NewLine);
            continue;
        }
        this.m_xlWBook = this.m_xlApp.Workbooks.Open(strFileName, 3, true, misVal, misVal, misVal, misVal, misVal, misVal, false, misVal, misVal, misVal, misVal, misVal);
        if (Make(ref m_xlWBook, m_strDstPath, ref m_ListIgnoreSheetNames) != true)
        {
            this.m_ParentWnd.Log("-!- Error while making " + m_xlWBook.Name + " into a csv file" + Environment.NewLine);
            continue;
        }
        this.m_ParentWnd.Log("Completed " + m_xlWBook.Name + Environment.NewLine);
    }
    catch (System.Exception e)
    {
        m_ParentWnd.Log(e.ToString());
    }
    this.m_xlWBook.Close(false, null, null);
}
this.m_xlApp.Quit();
Common.releaseObject(this.m_xlApp);
+3  A: 

The problem, at least from what I see from your code, is that you are not closing Excel once your are done (app.Quit) - and if you are, you are probably not disposing of the objects you create. With Excel interop you are out of the happy garbage collection, and you have to be careful to clean up after yourself. One tiny forgotten instance will keep Excel going in the background with the annoying side-effects you describe. Check out this StackOverflow thread for an extensive discussion on this: http://stackoverflow.com/questions/158706/how-to-properly-clean-up-excel-interop-objects-in-c

Mathias
thank you very much! I was releasing the com object without quit();.It solves my problem of zombie process.But is there a way to block access to my c# program created instance of excel by user? I'd appreciate it much.
Die4ACause