views:

976

answers:

8

I have a Windows Forms application that I wrote that does some monitoring of an inbox and database. The customer has informed me that it needs to run every time the server reboots. Shame on me for letting the customer change the requirements.

I was thinking ... is there any way to make a Windows Forms application run as a service so that it starts automatically?

Clarification

I would like to try to not have to write any more code ... if possible!

A: 

The only way to even consider this is to make sure that the application has no UI elements to it, as you have to jump through hoops on a non-Vista machine to make this work, and on Vista, you can't interact with the desktop at all.

Rather, you would be best off refactoring out the functionality into a set of shared libraries, and then create a service that uses those libraries, and install that at the client.

casperOne
Hmmm ... so there is no way to go into Windows Services and make it run my application like a service?
mattruma
Nop, a service require a class derived from ServiceBase class and a ServiceInstaller class to install in Windows Services.
Ricardo Villamil
A: 

If you have a nice decoupled functionality in you forms application, it should be straightforward to create a service class with its installer and then launch your processor class in the OnStart method of the service:

 protected override void OnStart(string[] args)
 {
                    Processor processor = new Processor();
  Thread workerThread = new Thread(processor.OnStart);
  workerThread.IsBackground = true;

  try
  {
   workerThread.Start();
  }
  catch
  {
                      //...
  }
 }
Ricardo Villamil
A: 

Found this article http://support.microsoft.com/kb/137890 called How To Create a User-Defined Service.

mattruma
That should work, but it seems to not be a very serviceable solution. The link I posted should provide a less messy approach.
EnocNRoll
EnocNRoll is right! Do not use the stuff from that article. It has long been outdated and gives little real value.
Stephen Martin
A: 

You can use InstallUtil to install your app as a service, but you need to make sure it's not reliant on the GUI, and I would recommend you change the startup of the app so it doesn't attempt to create any forms.

Steven Robbins
To use InstallUtil you need a class derived from Installer, you also need a ServiceProcessInstaller() and ServiceInstaller() objects defined in your installer class. InstallUtil looks for these in your exe project.
Ricardo Villamil
Maybe it's not InstallUtil I'm thinking of then.. I've definately installed a "normal" app as a service several times using a command line util. SC rings a bell.
Steven Robbins
+4  A: 

Start Your Windows Programs From An NT Service

Check this article out: http://www.codeproject.com/KB/system/xyntservice.aspx

It's a really old article, but it has worked for 8 years and it continues to be kept up to date by the author.

It will do what you want to do.

EnocNRoll
I found something similar that comes with the Windows Resource Kit ... http://support.microsoft.com/kb/137890
mattruma
I think it is still important to test this because it may be that the GUI aspect of the application may falter when it is launched if a user is not logged in to the server. In any previous scenarios I have had like this, the administrators would set up auto-login scripts and simply launch the exe.
EnocNRoll
Note: I acknowledge that this is a hack, but that is what MattRuma is looking for in this case.
EnocNRoll
How did this end up at 1 vote again? I voted it up to 2 a couple hours ago and I can't see any reason to down vote it.
Stephen Martin
yeah, I know, really. I wondered the same thing.
EnocNRoll
+1  A: 

You can run a winforms application as a service, you just won't be able to see it-- it will be displayed on a so-called virtual desktop, which can't be viewed on your monitor.

MatthewMartin
This is what I am looking to do.
mattruma
ok, cool, then there are no worries about launching a UI Thread without a logon context? great! Then either of the two articles posted thus far should work, theoretically.
EnocNRoll
+3  A: 

If you are sure the application can run unattended safely (by this I mean it can never throw up a modal UI element like a Message Box) and it doesn't need any interaction until shutdown, where it will simply be terminated, then run it as a scheduled task with the trigger set to system start up.

If it can run unattended but it may need to be shutdown and restarted manually or it can't just be terminated at shutdown then use the XYNTService as recommended by EnocNRoll. It's a horrible hack but it will work for what you want.

But by far the best solution is to separate the functionality of your program from the User Interface and write a proper service. And for a production server I wouldn't allow anything else. If it isn't easy to separate then you have some design issues you should look into anyway.

Stephen Martin
I've assumed here that MattRuma is simply looking for a quick way to make this happen. Otherwise, I totally agree. However, the battle to do things the right way is hardly ever won after something is in production.
EnocNRoll
Yes ... I am looking for the quick and dirty ... I don't have the luxury or time to do this right ... at least not right now.
mattruma
Yeah, I know how that feels. Just use XYNTService or the Task Scheduler then but really check your app very carefully for any possible unattended UI problems.
Stephen Martin
A: 

Hi mattruma.

You may also want to consider AlwaysUp, a commercial application that will run any application as a Windows Service. It is similar to XYNTService (mentioned by EnocNRoll), but has more features and is fully supported.

Good luck!

CoreTech