Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / October 2006

Tip: Looking for answers? Try searching our database.

System.Timers.Timer stops firing event

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rui Pedro Sousa - 17 Oct 2006 19:09 GMT
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;
}
Bryan Phillips - 17 Oct 2006 19:31 GMT
What classes are you using in the ProcessMessage method?  I had problems
with hanging a windows service while I tried using the PrintDocument
class.

Bryan Phillips
MCSD, MCDBA, MCSE
Blog:  http://bphillips76.spaces.live.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
[quoted text clipped - 60 lines]
> return threadID;
> }
Rui Pedro Sousa - 18 Oct 2006 11:10 GMT
Hi Bryan,

Thanks for your reply.

The MessageProcessor Class reads a MSMQ Queue, which can be a great part of
the problem because there is some unmanaged code behind System.Messaging.

It also writes stuff to SQL using the Data Application Block.

I'll post some code.

> What classes are you using in the ProcessMessage method?  I had problems
> with hanging a windows service while I tried using the PrintDocument
[quoted text clipped - 68 lines]
> > return threadID;
> > }
sloan - 17 Oct 2006 22:02 GMT
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

> 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
[quoted text clipped - 60 lines]
> return threadID;
> }
Rui Pedro Sousa - 18 Oct 2006 11:28 GMT
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. ;)

> I prefer to "code up" my timers.
>
[quoted text clipped - 112 lines]
> > return threadID;
> > }
sloan - 18 Oct 2006 15:09 GMT
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.

> Hi sloan,
>
[quoted text clipped - 52 lines]
> >
> > HEre is C# code also:

http://msdn2.microsoft.com/en-us/library/system.threading.timercallback.aspx

> > > I have a Windows Service with a Timer object with an interval of 500 ms,
> > in
[quoted text clipped - 63 lines]
> > > return threadID;
> > > }
Morten Wennevik - 18 Oct 2006 09:24 GMT
Hi,

There are some articles on the net describing that you should never use  
System.Timers.Timer in a windows service, although I have yet to find out  
why.  I have, however, personal experiences with System.Timers.Timer not  
working in a windows service.

The solution for this is to use System.Threading.Timer

Signature

Happy Coding!
Morten Wennevik [C# MVP]

Rui Pedro Sousa - 18 Oct 2006 11:26 GMT
Hi Morten,

That was it. :)

When developing my service I followed some articles like this one
(http://www.codeguru.com/columns/dotnet/article.php/c6919/) that uses
System.Timers.Timer inside a Windows Service.

But with a more directed search I finnally found an KB article that explains
everything. (http://support.microsoft.com/kb/842793)

Thanks.

> Hi,
>
[quoted text clipped - 4 lines]
>
> The solution for this is to use System.Threading.Timer

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.