Windows service timer problem

  • Thread starter Thread starter Scott H
  • Start date Start date
S

Scott H

Hello,
I'm having a go at writing my first Windows Service in VB.NET and I'm
having a problem. I have it installed ok and started the service
sucessfully, I can stop it, and restart it fine, the problem is it
doesn't appear to be running any of my code whatsoever.

I dropped a Timer Control on the designer, set it to disabled with an
interval of 5000, went to the code window and on the OnStart Event,
typed in:
Timer1.Enabled=True

But the .Tick event never fires at all. I've tested this out by
putting a MsgBox in the Onstart event, and sure enough the service
stops right away becuase of that. I then cut it out of there, and put
it as the first thing in Timer1's .Tick event, the service runs
without stopping itself...its not hitting the Msgbox

I even tried another approach, creating a new thread in the OnStart
event, and Starting the thread, but again, the thread never starts.

It will run anything I put in the Onstart event, unless it involves
running any other Subs or Functions.

Any Help?

Scott
 
I'm still stuck, someone must have written a windows Service in VB.NET
to get back to basics, I made this...

Protected Overrides Sub OnStart(ByVal args() As String)
Timer1.Enabled = True
End Sub

Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As_
System.EventArgs) Handles Timer1.Tick
'this event is never fired
End Sub

(created as a window service)
It compiles, installs and I can start the service, but Timer1_tick is
never fired. I put the Timer1 onto the designer window rather than
create one in code.


Scott
 
I dropped a Timer Control on the designer, set it to disabled with an

Which timer control? There are several available. The Windows Forms Timer
is probably not appropriate. Try the System.Timers.Timer
it as the first thing in Timer1's .Tick event, the service runs
without stopping itself...its not hitting the Msgbox

Is the service set up to allow interaction with the desktop? If not, the
msgbox code may be executing but you just may not see it.

To debug, try setting the timer interval to something larger like 10
seconds then start the service and then attach the debugger to it and put a
breakpoint inside the timer tick code. That should show if it is hitting
the timer code or not.
 
You have to create the timer in code;

Private mvTimer As New System.Timers.Timer(5000)

Protected Overrides Sub OnStart(ByVal args() As String)
AddHandler mvTimer.Elapsed, AddressOf mvTimer_Elapsed
mvTimer.Enabled = True
' or mvTimer.Start (not sure of the exact difference yet...)
End Sub

Private Sub mvTimer_Elapsed(ByVal pSender As Object, ByVal pArgs As
System.Timers.ElapsedEventArgs)
'this event had better fire!!
MessageBox.Show("I fired!!", "Yay!", MessageBoxButtons.OK,
MessageBoxIcon.Information)
End Sub

(add mvTimer.Enabled = False (or mvTimer.Stop) and RemoveHandler... to Sub
OnStop for completeness)
_____________________________
The Grim Reaper
 
Back
Top