How To Convert a WinForms App To A Windows Service
August 14th, 2007
In my previous article, How To Make Debugging .NET Windows Services Easier, I mentioned using a console or WinForms app to scaffold a Windows Service app for debugging and initial development. Unfortunately, there is a little ‘gotcha’ that needs to be addressed if you decide to do this.
When you’re ready to convert, you’ll want to set the startup object of your Windows Service to the name of your class that contains the service code, including the standard service Protected Overrides OnStart, OnStop, etc., and remove any test forms or Sub Main code from your project. What you will see in the My Project Application tab after doing this is that the old startup objects will remain on the list while your desired class isn’t on the list as shown here:

So, what can you do to correct this problem?
To handle this, you’ll need to edit a hidden generated Designer file because when you switch project types, certain code is not automatically generated. To get to the hidden file, bring up the Solution Explorer window and click on the ‘Show All Files’ button. It’s at the top and is the button circled in red below:
Next, you’ll want to expand the your service’s module to reveal the Designer module, as circled in blue above. Open this file and add the following code to it:
' The main entry point for the process
<MTAThread()> _
<System.Diagnostics.DebuggerNonUserCode()> _
Shared Sub Main()
Dim ServicesToRun() As System.ServiceProcess.ServiceBase
' More than one NT Service may run within the same process. To add
' another service to this process, change the following line to
' create a second service object. For example,
'
' ServicesToRun = New System.ServiceProcess.ServiceBase () {New Service1, New MySecondUserService}
'
ServicesToRun = New System.ServiceProcess.ServiceBase() {New MyService}
System.ServiceProcess.ServiceBase.Run(ServicesToRun)
End Sub
Of course, you would replace ‘MyService’ with the name of your service class. Now, when you go back to the My Project Application tab and click on the StartUp Object drop down you’ll see your service class on the list. Select it and you’ll be ready to compile your application as a Windows Service.
Please let me know if this article was helpful to you or if you have any other questions about this or other Windows Service issues by leaving me a comment.
Entry Filed under: Tip Sheets
Rate This Article:









(4 votes, average: 4.5 out of 5)
6 Comments Add your own
1. Alyson | August 18th, 2007 at 8:11 pm
hi nice post, i enjoyed it
2. Aaron Throckmorton | September 19th, 2007 at 2:41 pm
Fantastic article. This is exactly what I needed. Without this article I’d still be maintaining separate projects, and copying code back and forth. Thanks!
3. jfrankcarr | September 19th, 2007 at 3:17 pm
Thanks. I’m glad you found it helpful.
4. laura | June 26th, 2008 at 9:55 am
Thanks exactly wat i was lookin for
5. Arthur | December 23rd, 2008 at 7:41 pm
short and sweet! Just what I’m looking for. Thanks
6. Jim | July 28th, 2009 at 3:01 pm
Quite interesting… but how do I get a WinForms application to communicate with a Windows Service?
Leave a Comment
Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
Trackback this post | Subscribe to the comments via RSS Feed