Ok, I haven't even tried this so I'm not sure if it will work, but you could try something along these lines:
Microsoft.Office.Interop.Excel.Application excel = ...;
System.Diagnostics.Process excelProcess = null;
var processes = System.Diagnostics.Process.GetProcesses();
foreach (var process in processes)
{
if (process.Handle.ToInt32() == excel.Hwnd)
excelProcess = process;
}
excelProcess.Exited += new EventHandler(excelProcess_Exited);
UPDATE
Try the following code (its not pretty):
Microsoft.Office.Interop.Excel.Application xlsApp;
Process xlsProcess;
Timer timer;
public Form1()
{
xlsApp = new Microsoft.Office.Interop.Excel.Application();
xlsApp.Visible = true;
foreach (var p in Process.GetProcesses()) //Get the relevant Excel process.
{
if (p.MainWindowHandle == new IntPtr(xlsApp.Hwnd))
{
xlsProcess = p;
break;
}
}
if (xlsProcess != null)
{
timer = new Timer();
timer.Interval = 500;
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
}
}
void timer_Tick(object sender, EventArgs e)
{
if (xlsApp != null && !xlsApp.Visible)
{
//Clean up to make sure the background Excel process is terminated.
xlsApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlsApp);
xlsApp = null;
}
if (xlsProcess.HasExited)
{
//The event is not fired but the property is eventually set once the process is terminated
this.timer.Dispose();
this.Close();
}
}
The problem when working with an Excel Application is that even if the user closes it's main window, the process will keep on running in the background. In order to release it completely you have to call Application.Quit
but you should only do that when you detect the user closing the window which is exactly the problem you are trying to solve...you have a circular reference here :D
Latching to the relevant System.Diagnostics.Process
doesn't seem to work either, as the Exited
event never gets fired (not even if you terminate the background process from the task manager). My guess is that this event is fired only with processes launched through process = Process.Start(...)
.
So the only solution I can see is polling if Excel`s main window is visible. Once it is not, that probably means the user closed the main window and the Excel application should be terminated (if there is some scenario where Excel's main window's visibility can be false other than when the user wants to terminate the process then this solution is not valid). From there it is pretty straightforward.