Run Windows Forms application as service?

(This answer is obsolete, since the linked article was deleted in 2012. However, StackOverflow does not allow the accepted answer to be deleted. So, kindly do not downvote this answer, since I have informed you that it is obsolete.)

Alt link: https://web.archive.org/web/20111221041348/http://www.codeproject.com/kb/system/xyntservice.aspx

C++ source (Or use the Browse Code tab on the article): https://web.archive.org/web/20111228050501/http://www.codeproject.com/KB/system/xyntservice.aspx

Unfortunately, the Zip file source code download does not work within the alternative links.

Start Your Windows Programs From An NT Service

Check this article out: http://www.codeproject.com/script/Articles/ArticleVersion.aspx?aid=719&av=49654

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.


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.