Closes a process that has a user interface by sending a close message to its main window.
[Visual Basic] Public Function CloseMainWindow() As Boolean [C#] public bool CloseMainWindow(); [C++] public: bool CloseMainWindow(); [JScript] public function CloseMainWindow() : Boolean;
true if the close message was successfully sent; false if the associated process does not have a main window, or the main window is disabled (for example if a modal dialog is being shown).
When a process is executing, its message loop is in a wait state, and executes every time a windows message is sent to the process by the operating system. Calling CloseMainWindow revokes all running message loops on all threads and closes all windows. The request to exit the process by calling CloseMainWindow does not force the application to quit. It can ask for user verification to perform the shutdown, or it can refuse shutdown if the application is running a macro. To force a shutdown, use Kill. The behavior of CloseMainWindow is identical to that of a user closing an application's main window using the Close button. Hence, 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 only be used when necessary. CloseMainWindow enables an orderly shutdown of the process and closes all windows: it is preferable for applications with an interface. If CloseMainWindow fails, you may use Kill to terminate the process. Kill is the only way to terminate processes that do not have graphical interfaces.
Note Currently, all processes started using a Process component open in a command window. They contain a message loop and you can attempt to close them using CloseMainWindow. This will change in a future release. Depending on how the non-graphical interface process is coded, CloseMainWindow could fail and Kill could be necessary in order to stop the process.
You can only call Kill and CloseMainWindow on processes that are running on the local computer. For security reasons, you cannot cause processes on remote computers to exit. You can only view information for processes running on remote computers.
Process Class | Process Members | System.Diagnostics Namespace