C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




How to run a service as a windows application


Posted Date: 01 Mar 2007    Resource Type: Code Snippets    Category: Windows Services

Posted By: Tony John       Member Level: Gold
Rating:     Points: 10



Windows services are usefull to run any process un attended. However, it is a pain to debug and test services during development. This code sample shows how to run your application as either a Windows NT service or as Windows application based on a condition in the app.config file.

Since Windows services will not have references to System.Windows.Forms.Dll, you must add a reference to it from your project so that you can use Windows Forms.

Next step is, create an entry in your config file as shown below:






Now, look at your service class file and locate the code something like below:

Shared Sub Main()
Dim ServicesToRun() As System.ServiceProcess.ServiceBase
ServicesToRun = New System.ServiceProcess.ServiceBase() {New Service1}
System.ServiceProcess.ServiceBase.Run(ServicesToRun)
End Sub


The above piece of code starts your application as a Windows Service. All you have to do is, based on the key in your app.config, execute above code or launch the Windows Form.

Add new Form to your project called "MyForm". Now change the above code as shown below:


Dim runAs As String = System.Configuration.ConfigurationSettings.AppSettings("RunAs")
If runAs = "service" Then
Dim ServicesToRun() As System.ServiceProcess.ServiceBase
ServicesToRun = New System.ServiceProcess.ServiceBase() {New Service1}
System.ServiceProcess.ServiceBase.Run(ServicesToRun)
Else
System.Windows.Forms.Application.Run(New MyForm)
End If


The above code looks for the key in your app.config and based on teh value, it either starts the service or launches the window form application.

If you do not like to use the app.config entry, an alternative approach is to use the compiler directive #DEBUG


#If DEBUG Then
System.Windows.Forms.Application.Run(New MyForm)
#Else
Dim ServicesToRun() As System.ServiceProcess.ServiceBase
ServicesToRun = New System.ServiceProcess.ServiceBase() {New Service1}
System.ServiceProcess.ServiceBase.Run(ServicesToRun)
#End If


In this case, when the code is compiled in debug mode, it will run as a window application and run as a service in all other cases. In most cases, during development, you will be using DEBUG mode and RELEASE mode when deployed to QA or client.




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Start SQL Server service from your C# code
Previous Resource: Simple Windows Service
Return to Discussion Resource Index
Post New Resource
Category: Windows Services


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

conference calls

Contact Us    Privacy Policy    Terms Of Use