Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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?

/Thanks

Running code dem below:

C#
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");
        System.Threading.Thread.Sleep(11000);
        Console.WriteLine("Main Slept");
    }
}
Posted

1 solution

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!

Alan.
 
Share this answer
 

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


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900