Solved

VB.NET 2k5 Windows service timer not triggering

Posted on 2007-11-29
4
4,573 Views
Last Modified: 2013-11-26
Hi,

I have written a simple windows service that starts a timer in the OnStart event, the timer.Tick handler then performs the main service code every interval x.
I have the service installed under windows and I can attach the debugger, it is all working as expected except for this quirk (both with debugger attached, and not):
The timer only fires while the app is in the OnStart sub ie. this code fires the timer handler 3 times and no more (GiveTime is just a loop of DoEvents):

    Protected Overrides Sub OnStart(ByVal args() As String)
        tMain.Interval = 3000
        tMain.Enabled = True
        MyFuncs.GiveTime(9)
    End Sub

This code never fires the handler at all:
    Protected Overrides Sub OnStart(ByVal args() As String)
        tMain.Interval = 3000
        tMain.Enabled = True
    End Sub

My conclusion at this point is that the timer stops firing it's 'tick' event as soon as OnStart has completed and from what I understand this should not happen. Any ideas on this are welcome
0
Comment
Question by:Alw1n
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 18

Accepted Solution

by:
jcoehoorn earned 125 total points
ID: 20375476
.Net includes two completely separate timers:  system.windows.forms.timer and system.timers.timer.  You're probably using a forms timer, which is somewhat bound to the UI thread.  Try using a system.timers.timer instead.  It works pretty much the same, but has an elapsed event instead of a tick event.
0
 
LVL 18

Expert Comment

by:jcoehoorn
ID: 20375493
One more thing to add:  make sure your timer is defined in a place that doesn't go out of scope when your OnStart method has finished.
0
 
LVL 5

Expert Comment

by:mydasx
ID: 20375645
this is a scope problem.  System.Timers.Timer when started, runs in its own thread.  So your concept will work in this context.  Your issue seems to be that the timer is losing scope at the end of the method.  Where and how are you instantiating your timer?
0
 

Author Comment

by:Alw1n
ID: 20380356
Thank you. I was using the forms timer, the system timer works just fine.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question