Click here to Skip to main content
13,052,714 members (63,452 online)
Rate this:
Please Sign up or sign in to vote.
I have a System.Timers.Timer and I found out that in the ElapsedEventHandler an exception sometimes occured but the process did *not* die but the thread executing the ElapsedEventHandler died quitly where exception occured. Not good since I then wasn't aware about the exception. Why isn't the process terminated? How could I get the process terminated in best way in case an exception occurs in the ElapsedEventHandler?

I tried similar using a System.Threading.Timer and then the process terminated. Can you explain the difference?


Running code dem below:

class Program
    static public void DoBad()
        Console.WriteLine("DoBad enter");
        string s = "hello";
        object o = s;
        int i = (int)o; // Ops! Something unexpected happend here... Exception
        Console.WriteLine("DoBad exit");
    static void DoIt1(object sender, System.Timers.ElapsedEventArgs e)
        Console.WriteLine("\nDoIt1 enter");
        DoBad(); // Process is NOT terminated when excpetion occurs in DoBad, thread just dies quietly but process continues. Why is process not terminated?
        Console.WriteLine("DoIt1 exit");
    static void Test1()
        System.Timers.Timer t = new System.Timers.Timer(2000);
        t.Elapsed += new System.Timers.ElapsedEventHandler(DoIt1);
        t.Enabled = true;
    static public void DoIt2(Object o)
        Console.WriteLine("\nDoIt2 enter");
        DoBad(); // Process terminated when exception occurs in DoBad.
        Console.WriteLine("DoIt2 exit");
    static void Test2()
        System.Threading.Timer timer = new System.Threading.Timer(DoIt2, null, 0, 2000);
    static void Main(string[] args)
        Test1(); // Process NOT terminated when exception occurs in DoBad
        // Test2(); // Process terminated when exception occurs in DoBad
        // Why is process NOT terminated when running Test1 (but terminated when running Test2)?
        // Want process to die. How to do it in best way?
        Console.WriteLine("Main Sleep");
        Console.WriteLine("Main Slept");
Posted 12-Oct-12 21:35pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

I was looking into this yesterday when I had a similar problem. If you open the published source code Timer.cs[^] and scroll down to the MyTimerCallback method you'll see that the event invocation has an empty catch block.

Although I had never noticed it before, the behaviour is documented Elapsed Event[^] (see the remarks section). I wonder if the comment about this behaviour being "subject to change in later versions of the framework" is a get out in case they ever want to fix their mistake!


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 13 Oct 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100