Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Windows Forms application as service?

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!

like image 331
mattruma Avatar asked Jan 20 '09 20:01

mattruma


4 Answers

(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.

like image 62
EnocNRoll - AnandaGopal Pardue Avatar answered Oct 29 '22 06:10

EnocNRoll - AnandaGopal Pardue


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.

like image 37
Stephen Martin Avatar answered Oct 29 '22 04:10

Stephen Martin


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.

like image 1
MatthewMartin Avatar answered Oct 29 '22 05:10

MatthewMartin


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!

like image 1
CoreTech Avatar answered Oct 29 '22 04:10

CoreTech