views:

141

answers:

4

HI Guys, I have created a windows service which spawns three threads.The first thread wakes up every 15 sec, the second thread wakes up every min. and the third thread once in a day. My Code looks something like this:

        var timer1 = new Timer();
        timer1.Elapsed += ProcessTimerEvent1;
        timer1.Interval = 60000;
        timer1.Start();

        var timer2 = new Timer();
        timer2.Elapsed += ProcessTimerEvent2;
        timer2.Interval = 15000;
        timer2.Start();

        var timer3 = new Timer();
        timer3.Elapsed += ProcessTimerEvent3;
        timer3.Interval = 86400000;
        timer3.Start();

From my event logs I can see that it is saying .NET Runtime 2.0 Error Reporting EVENTID:5000.

I looked through the net and it says invalid operationexception.

DO you guys think whether this stopping of service has to do anything with threads. And the other silly question is am I spawning 3 new threads everytime or the same threads gets up evey 15 sec or 1 min.

+2  A: 

Probably one of your threads is throwing an unhandled exception. This will let your process die immediately. Make sure that you handle any exception inside your threads at some point by wrapping the code inside the thread into try-catch-blocks (and don't forget to log properly so that you can be aware of the things that go wrong).

0xA3
A: 

It is probably due to an exception in the code in one of your events.

Shiraz Bhaiji
A: 

are you keeping a reference of the timers on some global (static) object?. If not even if they are still running they are candidates for garbage collection. The CG will call their destructors witch in turn will call the Dispose method and your threads will be abruptly stopped.

For the secons question; the timers use the thread pool to spawn threads. Each processing will grab a thread from the pool, use it, and the release it. Between periods they just wait for notifications from the System clock.

AZ
A: 

Alice,

You can add Unhandled Exception handling to your service with code like this: -

namespace YourNamespace
{
    static class Program
    {

        [STAThread]
        static void Main()
        {
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }

        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            HandleException(e.Exception);
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            HandleException((Exception)e.ExceptionObject);
        }

        static void HandleException(Exception e)
        {
            //Handle/Log Exception Here
        }

    }
}

Thanks,

Phil

http://exceptioneer.com

Plip