Closing tray applications from .NET

  • Thread starter Thread starter Dmitry Duginov
  • Start date Start date
D

Dmitry Duginov

I'm trying to make a utility that should assist backup procedure by closing
multiple applications sitting in systray. The problem I bumped into is that
many of those applications do not close their main window if I try to call
Process.CloseMainWindow(). What are the other "polite" methods to close such
applications? (except, of cource Process.Kill())

Is there a way to send the same message that closes all the applications
upon Logoff, but selectively, only to some specific applications?

Regards,
Dmitry
 
Hi Dmitry,

Based on my understanding, you want to terminate the process by using
CloseMainWindow method. But it seems that it did not work with all the
application.
I think this is because the CloseMainWindow will send the WM_CLOSE message
to main window of the application, but the application may not close the
application, but ignore or do something else. So the application is not
terminated.

So far I think you may try to send the WM_DESTROY to the application's
mainwindow.
Please check the code below.

private const int WM_DESTROY = 0x2;
private const int WM_QUIT = 0x12;
[DllImport("user32.dll")]
private static extern int PostMessage(IntPtr hwnd, int wMsg, int
wParam, int lParam);
private void button1_Click(object sender, EventArgs e)
{
Process[] ps = Process.GetProcessesByName("TestWin32App");
if (ps.Length > 0 && ps[0] != null)
{
PostMessage(ps[0].MainWindowHandle, WM_DESTROY, 0, 0);
//PostMessage(ps[0].MainWindowHandle, WM_QUIT, 0, 0);
}
}

We can also try to send the WM_QUIT messsge if the WM_DESTROY did not work,
but that is not recommended, because that should be sent from the
Application via PostQuitMessage.
WM_QUIT Notification
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui
/windowsuserinterface/windowing/windows/windowreference/windowmessages/wm_qu
it.asp

For the LogOff steps, the system will send WM_QUERYENDSESSION, which
commonly was not sent by user.
Logging Off
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shutdown/ba
se/logging_off.asp

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
"Peter Huang" said:
Hi Dmitry,

Based on my understanding, you want to terminate the process by using
CloseMainWindow method. But it seems that it did not work with all the
application.
I think this is because the CloseMainWindow will send the WM_CLOSE message
to main window of the application, but the application may not close the
application, but ignore or do something else. So the application is not
terminated.

So far I think you may try to send the WM_DESTROY to the application's
mainwindow.
Please check the code below.

private const int WM_DESTROY = 0x2;
private const int WM_QUIT = 0x12;
[DllImport("user32.dll")]
private static extern int PostMessage(IntPtr hwnd, int wMsg, int
wParam, int lParam);
private void button1_Click(object sender, EventArgs e)
{
Process[] ps = Process.GetProcessesByName("TestWin32App");
if (ps.Length > 0 && ps[0] != null)
{
PostMessage(ps[0].MainWindowHandle, WM_DESTROY, 0, 0);
//PostMessage(ps[0].MainWindowHandle, WM_QUIT, 0, 0);
}
}

We can also try to send the WM_QUIT messsge if the WM_DESTROY did not work,
but that is not recommended, because that should be sent from the
Application via PostQuitMessage.
WM_QUIT Notification

For those tray applications that cannot be closed using
Process.CloseMain.Window(), WM_DESTROY doesn't work either. WM_QUIT only
works if the application window retrieved from Systray before. I'm not sure
how to do that programmaticaly
 
Hi Dmitry,

Based on my understanding, if you have click the systray icon to pop up the
application windows and then minimize it back into the system tray, then we
can close the application by sending WM_QUIT event to the application's
main window.
If I have any misunderstanding, please feel free to post here.

So I think before we open the window and minimize it back into systray, the
window's message bump is not active, so the WM_QUIT event did not work.

Commonly we sending WM_CLOSE or WM_DESTROY to close an application normally
because the application will handle the WM_CLOSE or WM_DESTROY normally.
But for some specially applcation, they may be just ignore it or minimize
it.

Send WM_QUIT is not a recommended approach, because it will do the job
similar with Process.Kill which will terminate the main message loop
immediately.
So I think we can use Process.Kill approach here to simplify the process.

I understand that the Process.Kill will terminate the process immediately.
Commonly we did not use it, but use other normal approach which need to the
target application to go the normal way. But due to different design of the
target application, it is hard to use a common normal terminate approach.

e.g. if the application will handle the WM_CLOSE message normally, then we
can send the WM_CLOSE to the application, so that the application will do
the normal exit process to save the necessary information.

If you still have any concern, please feel free to post here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top