Question Advantages and disadvantages of Windows Services??

spudshed

New member
Joined
Jul 12, 2010
Messages
1
Programming Experience
5-10
Hi,
First post, so apologies if this has been asked before (I did have a search first!) :)

We have a project coming up and I would like to use Windows Services but need to convince some other developers of this approach first.

What are the advantages of using services over a conventional forms app?

I'm aware of;
1) Not needing to logon
2) Security - can run under a specific username
3) Recovery Options
4) Dependencies

Are there any other benefits? Performance?

Any disadvantages aside from services not being that easy to debug?

OS will be we Windows Server 2003/8. App will be some heavy duty number crunching. Data feed approx 4/5 times a second 24*7.

Thanks in advance!
 
If you are talking about a real-time application like this it almost demands being written as a service. The fact that it will start automatically on server reboot is reason enough. As for Pros/Cons...
Pros: as you already mentioned - the biggies are
1) auto start without user logon, and
2) Service Recovery Options if your service crashes, it can restart automatically
Also..
3) Performance should be comparable with a windows forms app, but not inherently better
4) Remote service control. Services can be started/stopped from remote computers without logging on and clicking. You can use net start, or any number of computer management tools to remotely start/stop your service. With forms apps, you have to log into the remote computer to start/stop. May not be a big deal for one instance, but if your service is installed on multiple servers, this can be a big time saver.

Cons: they are a little harder to work with
1) harder to debug b/c you have to attach to a running process. I usually set a Threading.Timer in the OnStart routine to pause and give me a few seconds to connect the debugger before processing starts
2) Cannot have UI interaction. That means any time that you would normally pop a message box (either to a user or for status messages) they must either be emailed or logged to some file or event log. Any UI message that remains in the code will still pop up (invisible to any logged in user) and stay there waiting for a user to click "OK" or whatever. If that message is blocking the app from continuing, then the only thing you can do is terminate the service. This point is very important to remember if you are using 3rd party controls b/c if they try to put any messages to the screen, they can hand your service completely.

These are what I can pull off the top of my head. I Always prefer to write services b/c I dont want to be surprised by the server being rebooted and my apps not running.
Hope that helps!
 
Amen to the warning re showing blocking dialogs from a service. I haven't found services particularly difficult to debug however. My method is:

I use C#, so you'll have to replicate this into VB. All C# programs start from what VBers call a "Sub Main"
When you chose your project's startup object to be a Form, the IDE writes (and hides) the bit of code that makes the form and calls Application.Run on it (which starts a message loop etc)

Instead you'll tell the project you want a sub main start, and than have a Sub Main like this (this is converted from c#)

VB.NET:
#If DEBUG Then
<STAThread> _
Private Shared Sub Main()
	Try
		Dim c As New YourServiceCore()
		c.OnStart(Nothing)
		'opens a blank form on screen. the 'service' stops when the form is closed
		System.Windows.Forms.Application.Run(New Form())
		c.OnStop()
	Catch ex As Exception
		EventLog.WriteEntry("Debug YourService", ex.StackTrace)
	End Try
End Sub




#Else
''' </summary>
Private Shared Sub Main()
	Try
		Dim ServicesToRun As ServiceBase()

		' More than one user 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 ServiceBase[] {new Service1(), new MySecondUserService()};
		'
		ServicesToRun = New ServiceBase() {New YourServiceCore()}

		ServiceBase.Run(ServicesToRun)
	Catch ex As Exception
		EventLog.WriteEntry("YourService", ex.[GetType]().ToString() + ":" & vbCr & vbLf + ex.StackTrace)
	End Try
End Sub
#End If

Depending on whether your IDE is in debug mode or release mode dictates whether you get a debug window (and your app starts like a normal windowed app) or a service

Note that this might not accurately represent the environment your service starts in (a desktop windows account started windowed app has different permissions to a service), so for advanced tuning of that adjust the project properties to start the app as a certain user etc.. but for my services it has always sufficed.
 
Back
Top