views:

280

answers:

2

I have a program that needs to sit in the background and when a user connects to a RDP session it will do some stuff 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 like this I have the terminal server launch this application. I have it set as a windows forms application and my code is this

public static void Main()
{
    //Do some setup work
    Process proc = new Process();
    //setup the process
    proc.Start();
    proc.WaitForExit();
    //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?

+2  A: 

You can create a hidden window that you use to handle the messages.

using System;
using System.Windows.Forms;

namespace WindowsApplication1
{
  class Program
  {
    [STAThread]
    static void Main(string[] args)
    {
      Application.Run(new MessageWindow());        
    }
  }

  class MessageWindow : Form
  {
    public MessageWindow()
    {
      this.ShowInTaskbar = false;
      this.WindowState = FormWindowState.Minimized;
      // added by MusiGenesis 5/7/10:
      this.FormBorderStyle = FormBorderStyle.FixedToolWindow;
    }

    protected override void WndProc(ref Message m)
    {
      base.WndProc(ref m);
    }
  }
}  
Chris Taylor
Tried that, it shows up in Alt-Tab. That is my backup solution but I would really like one that does not show up in the alt-tab menu.
Scott Chamberlain
If you have this set as a Windows application it should not show up in Alt-Tab.
Chris Taylor
I added a line that will make this work (and keep the window out of the Alt-Tab list).
MusiGenesis
+2  A: 

See this question: http://stackoverflow.com/questions/357076/best-way-to-hide-a-window-from-the-alt-tab-program-switcher

I tried all of the solutions, but no matter what I do the window still shows up in the Alt-Tab list (I'm running Vista).

In Windows Mobile, you set a form's Text property to blank to keep it out of the running programs list (the WinMo equivalent of the alt-tab list). Perhaps this will work for you, but I doubt it.

Update: OK, this does work after all. If you create and show a form with its FormBorderStyle set to FixedToolWindow and its ShowInTaskbar set to false it will not appear in the Alt-Tab list.

MusiGenesis