Close Excel instance when Windows Form is closed by user in a VB 2008 application
Posted on 2012-04-06
I'm currently developing an application using Visual Basic, in Visual Studio 2008 Professional, on a Windows XP machine. However, we have both XP and Windows 7 client workstations in our office.
In my application, I'm exporting the results displayed in a DataGridView control to MS Excel, using the methods in the Microsoft.Office.Interop.Excel namespace. The procedure which opens the instance of Excel and exports the data is running in a separate thread from the form which contains the DataGridView control. This is working well.
However, if the user closes the form before the export is completed I want to cancel the thread and close the instance of Excel without saving it. I can use the Thread.Abort method in my form's FormClosing event handler to kill the thread, but the Excel process is still hanging out there even after the application is closed.
What would be the best way to identify and kill that Excel process if the user closes the form prior to the export being completed?