Killing an Excel process in debug mode

Hi all.

I have an application I’m working on and it has the standard VS generated InitializeComponent() and Form1 load() methods. I have a main() which has a try-catch-finally block like so:

try

{

Application.Run(new Form1());

}

catch(Exception e)

{

MessageBox.Show(e.Message.ToString());

}

finally

{

if(excelObject != null)

{

excelObject.Quit();

excelObject = null;

}

}

I have the C# application running in debug mode which has an opened Excel process. I have a method that catches and destroys the Excel object in my Dispose() method, but when I stop debugging, the Excel process remains running and I am have to use the Task Manager to manually close it. Where in my code can I close this Excel process when I stop debugging

Thanks!



Answer this question

Killing an Excel process in debug mode

  • Tony128

    How are you ending your debug session If you close the app, then the Garbage collector should eventually clean up - but if you press the 'Stop Debug' button in Visual Studio, then the Excel process will be orphaned and remain in the background.

    Excel is supposed to kill itself when it detects there are no more handles attached to it, but stopping your debug process rather than quitting the app will leave an invalid handle on Excel.

  • akka

    Hi,

    I had a similar problem. The instance will eventually dissapear, but you can make it do so straight away by forcing the GC. I know this isn't something you're really supposed to do, but it worked for me. So just add GC.Collect(); into your code the line after you mark your handle as null and it should clean it up.


  • subdigital

    Please check that if the excel object is not static, secondly make sure that there is not more than one excel documents opened.

  • force_fx

    Maybe this might help

    Process kill_excel = new Process();

    public Form1()
    {
    InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
    kill_excel.StartInfo.FileName = ("EXCEL");
    kill_excel.Kill();
    }

    private void button2_Click(object sender, EventArgs e)
    {
    kill_excel.StartInfo.FileName = ("EXCEL");
    kill_excel.Start();
    }



  • Killing an Excel process in debug mode