orcberg

I'm using VB.NET 2005, and I'm looking to put together a simple service. I already wrote this as a regular application, but I figure it will be a lot more useful as a service (where it always runs, and doesn't need to have a user logged in).

The idea behind the service is that it simply checks a few folders and the status of another service at a regular interval. To do this, I have a timer that fires every minute. The system itself works fine, the part that doesn't work now that I have it in the form of the service is that the service stops and immediately stops. I get a message that says "The XMonitor service on Local COmputer started and then stopped. Some services top automatically if they have no work to do, for example, the Performance Logs and Alerts service."

My OnStart sub looks like this:

Code Snippet

Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things
' in motion so your service can do its work.

If Not EVENT_LOGGER.SourceExists("XMonitor") Then
EVENT_LOGGER.CreateEventSource("XMonitor", "XMonitor Log")
End If

LoadConfiguration()

EVENT_LOGGER.Source = "XMonitor"
EVENT_LOGGER.WriteEntry("XMonitor service started.", EventLogEntryType.Information, 1000)

MainStart()

End Sub

All LoadConfiguration() does is load the interval to perform the checks with (in case the user wants to override the default). MainStart is what actually starts the timer. These functions are all in a separate module I created. The MainStart sub and the timer I use:

Code Snippet

Public WithEvents CheckTimer As New Timers.Timer

Public Sub MainStart()

CheckTimer.Enabled = True

CheckTimer.Interval = 60000

End Sub

The Tick event for the timer (for simplified, testing purposes):

Code Snippet

Private Sub CheckTimer_Tick(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles CheckTimer.Elapsed

MsgBox("Tick!")

End Sub

From what I've read, I believe that I'm doing everything properly -- but I must be missing something. This is the first Windows service I've tried to write, so forgive my newbishness. Wink

Any ideas



Re: Visual Basic General Service issues

Frank Carr

The most common cause of this kind of error is not giving the NETWORK SERVICE or LOCAL SERVICE user sufficient security to perform the requested task.




Re: Visual Basic General Service issues

orcberg

What user do these services run under It's not the IUSR or IWAM accounts, is it I read the article at http://technet2.microsoft.com/windowsserver/en/library/a958b9d2-3ec1-4ab0-9f9f-7cb396605f3e1033.mspx mfr=true but I'm still not sure I know where to find the account or how to identify it.



Re: Visual Basic General Service issues

Swade

Can you even display a MsgBox in a windows service

Try writing something to a text file.






Re: Visual Basic General Service issues

orcberg

Sorry about the delay in responding -- I got tied up with some other issues for a while.

I tried changing the OnStart code so it was very simply:

Code Snippet

Protected Overrides Sub OnStart(ByVal args() As String)

MsgBox("Test!")

End Sub

Unfortunately this makes no difference. The messagebox doesn't appear and it still gives me the same "started and then stopped" notice.





Re: Visual Basic General Service issues

orcberg

As a follow-up: I set this service to run as the Domain Administrator on the network. This account also has full control over the machine the service is running on. It still gives the same message about starting and stopping.

I also created a brand new service for testing purposes. The only thing this service contained was a MsgBox() in the OnStart sub. It also gives the same error message. I tried running it as Domain Administrator, local administrator, NETWORK SERVICE, LOCAL SYSTEM, and LOCAL SERVICE ... same problem on all of them. I can't imagine what permissions they all lack to have the service pop a simple message box up.





Re: Visual Basic General Service issues

Swade

As a repeat... a service is not meant to interact with a user. A MsgBox is probably not the best test for checking to see if a service will run as a MsgBox is of the Windows.Forms namespace and a service is not.

So, please, try writing something to a text file.

Code Snippet

Dim X as string = "Testing...."

My.Computer.FileSystem.WriteAllText("C:\Temp\test.txt", X)






Re: Visual Basic General Service issues

orcberg

Okay, I did that and it worked. So I then went to the regular application and made the change, removing any instance of MsgBox and putting some file output lines to try to track where the service was blowing up. Eventually, I found that it was dying on the lines:

Code Snippet

EVENT_LOGGER.Source = "XMonitor"

EVENT_LOGGER.WriteEntry("XMonitor service started.", EventLogEntryType.Information, 1000)

EVENT_LOGGER is simply an eventlog control that I added to the service. Once I commented out these two lines, the program no longer does the "start then stop" deal. I guess it's not a big deal because I can do logging to a text file, but I'd have preferred to do it in the Event Log if possible. Any ideas why it might be blowing up

Other than that, so far it seems to be working properly. Thanks for the assistance!





Re: Visual Basic General Service issues

orcberg

Oh, here's a weird thing. When I try to stop the service through the Services screen, it gives an error:

"Could not stop the XMonitor service on Local Computer. The service did not return an error. This could be an internal Windows error or an internal service error. If this problem persists, contact your system administrator."

My OnStop sub:

Code Snippet

Protected Overrides Sub OnStop()

DoLog(" *** " & GetDateNow() & " - Service stopped on """ & Environment.MachineName & """.")

CheckTimer.Enabled = False

End

End Sub

Note that I had originally tried it without the "End" in there to try to fix the same issue but it had no effect. If I try to stop it a second time after I get the error message, it takes forever and eventually gives up trying to stop the service (though when I refresh the Services list, it then shows up as stopped).





Re: Visual Basic General Service issues

Swade

Do you have Me.OnStop = False




Re: Visual Basic General Service issues

orcberg

No, I don't. Should I Where should it go





Re: Visual Basic General Service issues

Swade

No, you would want the OnStop property = True if you plan on using it. It can be changed in code or on the designer properties for the service.




Re: Visual Basic General Service issues

orcberg

I can't set Me.OnStop in the code -- it's a Protected Sub. However, in Properties for the service I do have CanStop set to True. It still takes forever for the service to stop. I have to stop it, click OK to the message that pops up and says it can't be stopped (it still runs the OnStop code however, because it writes the log file I set up saying that the service has been stopped), then stop it again. It then eventually stops the service. I'm not sure why it hangs like that.

As far as all of the service properties go:

AutoLog = True

CanHandlePowerEvent = False

CanHandleSessionChangeEvent = False

CanPauseAndContinue = False

CanShutdown = False

CanStop = True

ExitCode = 0

These all should be the defaults -- I haven't changed anything here.





Re: Visual Basic General Service issues

Swade

Well I don't really know then. The services I've created all stop in about the same time as they start. I wish I could help more.




Re: Visual Basic General Service issues

orcberg

I've finally resolved this problem. It looks like the stopping issue was a result of an issue with the Timer, specifically the Timer not stopping properly.

I had been declaring the Timer in the source file for the service itself, but I was using it in a module I added to the project. I moved the declaration of the Timer to the module. I then added a second .Stop() for the Timer to the OnStop() sub. Installed the new service executable and it stops almost immediately now, without popping up that error message.