sloan
Wed Oct 18 09:09:17 CDT 2006
Yeah,
I didn't like those "Drag and Drop" timers way back in VB4,VB5 or VB6.
Something told me to avoid them ..... when I was writing a Windows Service.
But thanks for the concrete reason at the KB.
You ought to email this shoney:
http://www.codeguru.com/columns/dotnet/article.php/c6919/
and let him know.
"Rui Pedro Sousa" <RuiPedroSousa@discussions.microsoft.com> wrote in message
news:DC9DE035-70EE-4DFB-B1C7-9BBF9C12440C@microsoft.com...
> Hi sloan,
>
> Your code is using the System.Threading.Timer class instead of the
> System.Timers.Timer class I was using.
>
> That was problem with my code, I was using the wrong class, as it is
> explained in this article:
http://support.microsoft.com/kb/842793
>
> Thanks. ;)
>
> "sloan" wrote:
>
> >
> > I prefer to "code up" my timers.
> >
> > I only have the vb.net version at the moment:
> >
> >
> >
> > Private m_doTheWorkTimer As Timer
> >
> >
> >
> > private sub RegisterTimer()
> >
> > Dim dueTime As Long
> >
> > Dim period As Long
> >
> > ' DUE TIME looks like it is the DELAY time .. from the time the service
> > starts,
> >
> > ' Until this specfic TIMER kicks in. So a Delay Time of 20000
> >
> > ' Will DELAY this timer shooting off until 20 seconds (20000 ms) after
the
> > service starts
> >
> > dueTime = 20000
> >
> > period = 5000
> >
> > ' the magic bullet, notice the part after "AddressOf", which is the
method
> >
> > Dim timerHandler = New TimerCallback(AddressOf BeepClass.GoBeep)
> >
> > m_doTheWorkTimer = New Timer(timerHandler , Me, dueTime, period)
> >
> > end sub
> >
> >
> >
> > Public Class BeepClass
> >
> >
> >
> > Public Shared Sub GoBeep(ByVal state As Object)'' this uses a built in
> > delegate, with "state as Object" as the generic argument list
> >
> > Beep()
> >
> > End Sub
> >
> >
> >
> > End Class
> >
> >
> > Outside of the wire up event handler, the C# conversion is easy.
> >
> > HEre is C# code also:
> >
> >
http://msdn2.microsoft.com/en-us/library/system.threading.timercallback.aspx
> >
> >
> >
> >
> >
> >
> > "Rui Pedro Sousa" <Rui Pedro Sousa@discussions.microsoft.com> wrote in
> > message news:FBE45079-5582-4DD1-A6A9-A5434D117F68@microsoft.com...
> > > I have a Windows Service with a Timer object with an interval of 500
ms,
> > in
> > > each event the windows service starts a new thread, up to a limited
number
> > of
> > > threads, and does some work.
> > >
> > > It works fine as to the users requirements, but sometimes freezes. The
> > > Service is running, its process is visible on Task Manager as usually,
no
> > > events on the Event Log are related to the service, but the work is
not
> > done.
> > >
> > > After some tests I foun out that the the code associated with the
OnTimer
> > > Event stops being executed with no apparent reason.
> > >
> > > I'm clueless.... :(
> > >
> > > Some code:
> > >
> > >
> > > public class QueueHandler : System.ServiceProcess.ServiceBase
> > > {
> > > private System.ComponentModel.Container components = null;
> > > private System.Timers.Timer timer = new System.Timers.Timer();
> > > private Thread[] serviceThreads;
> > >
> > > protected override void OnStart(string[] args)
> > > {
> > > serviceThreads = new Thread[25];
> > > int interval = 500;
> > > string autoThreads = "no";
> > >
> > > timer.Elapsed += new ElapsedEventHandler(OnTimer);
> > > timer.Interval = interval;
> > > timer.Enabled = true;
> > > }
> > >
> > > protected void OnTimer(Object Source,ElapsedEventArgs e)
> > > {
> > > int x = GetNextFreeThread();
> > >
> > > if (x >= 0)
> > > serviceThreads[x].Start();
> > > }
> > >
> > > private int GetNextFreeThread()
> > > {
> > > int threadID = -1;
> > >
> > > MessageProcessor mProcessor = new MessageProcessor();
> > > ThreadStart tStart = new ThreadStart(mProcessor.ProcessMessage);
> > >
> > > for (int y = 0;y<serviceThreads.Length;y++)
> > > {
> > > //if thread slot is free instaciate new thread
> > > if(serviceThreads[y] == null || serviceThreads[y].ThreadState ==
> > > System.Threading.ThreadState.Stopped)
> > > {
> > > threadID = y;
> > > serviceThreads[threadID] = new Thread(tStart);
> > > serviceThreads[threadID].Name = "QueueThread_" + y.ToString();
> > > serviceThreads[threadID].IsBackground = true;
> > > serviceThreads[threadID].Priority = ThreadPriority.Lowest;
> > > break;
> > > }
> > > }
> > >
> > > return threadID;
> > > }
> >
> >
> >