I have a program that needs to sit in the background and when a user connects to a RDP session it will do some environment setup then launch a program. When the program is closed it will do some housekeeping and logoff the session.
The current way I am doing it is I have the terminal server launch this application. This is built as a windows forms application to keep the console window from showing up:
public static void Main()
{
//(Snip...) Do some setup work
Process proc = new Process();
//(Snip...) Setup the process
proc.Start();
proc.WaitForExit();
//(Snip...) Do some housecleaning
NativeMethods.ExitWindowsEx(0, 0);
}
I really like this because there is no item in the taskbar and there is nothing showing up in alt-tab. However to do this I gave up access to functions like void WndProc(ref Message m)
So Now I can't listen to windows messages (Like WTS_REMOTE_DISCONNECT
or WTS_SESSION_LOGOFF
) and do not have a handle to use for for bool WTSRegisterSessionNotification(IntPtr hWnd, int dwFlags);
I would like my code to be more robust so it will do the housecleaning if the user logs off or disconnects from the session before he closes the program.
Any reccomendations on how I can have my cake and eat it too?