I\'m developing a vbnet/c#.NET based application that opens files with different applications(excel, word, etc).
The application is launched using Dim app As Proce
You can achieve behaviour close to what you require by calling Process.CloseMainWindow rather than Process.Kill
.
The behavior of
CloseMainWindow
is identical to that of a user closing an application's main window using the system menu. Therefore, the request to exit the process by closing the main window does not force the application to quit immediately.Data edited by the process or resources allocated to the process can be lost if you call
Kill
.Kill
causes an abnormal process termination, and should be used only when necessary.CloseMainWindow
enables an orderly termination of the process and closes all windows, so it is preferable for applications with an interface.
In the case of Office applications with unsaved changes, CloseMainWindow
would launch the Save dialog. You would need to handle scenarios where the users presses “Cancel”, since that may result in the WaitForExit
call blocking indefinitely.
For example:
// Launch Word application.
Process wordProcess =
Process.Start(@"C:\Program Files (x86)\Microsoft Office\Office12\winword.exe");
// Give user some time to type in text.
Thread.Sleep(TimeSpan.FromSeconds(20));
// Request Word to close.
wordProcess.CloseMainWindow();
// Wait until user saves or discards changes.
// May block indefinitely if user cancels.
wordProcess.WaitForExit();