开发者

How to handle exception using Timer (Thread) class

I'm trying to handle the Timer's exception. It would be nice if the class had something like HandlerExceptionEvent so that we could add some event to log something or stop the timer.

PS: I don't want to add a try/catch block inside ElapsedEventHandler().

class Program
{
static void Main(string[] args) {
  System.Timers.Timer t = new System.Timers.Timer(1000);
  t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);
  t.Start();     

  System.Threading.Thread.Sleep(10000);
  t.Stop();
  Console.WriteLine("\nDone.");      
  Console.ReadLine();
}

 static void t_Elapsed(object sender, 开发者_如何学运维System.Timers.ElapsedEventArgs e) {
   Console.WriteLine("Ping!");
   throw new Exception("Error!");
 }
}


PS: I don't want to add "try/catch Exception" inside ElapsedEventHandler()

Since the Timer class doesn't support such an event how would you otherwise catch an exception?

If you insist on using the Timer class then perhaps this is your only option:

var t = new System.Timers.Timer(1000);
t.Elapsed += (sender, e) => { 
    try 
    { 
        t_Elapsed(sender, e); 
    } 
    catch (Exception ex) 
    { 
        // Error handling here...
    } 
};

This way the actual handler t_Elapsed doesn't contain any error handling and you can create a wrapper class for the Timer class that hides this implementation detail and in turn provides an event for exception handling.

Here's one way to do that:

class ExceptionHandlingTimer
{
    public event Action<Exception> Error;

    System.Timers.Timer t;

    public ExceptionHandlingTimer(double interval)
    {
        t = new System.Timers.Timer(interval);
    }

    public void Start()
    {
        t.Start();
    }

    public void AddElapsedEventHandler(ElapsedEventHandler handler)
    {
        t.Elapsed += (sender, e) =>
        {
            try
            {
                handler(sender, e);
            }
            catch (Exception ex)
            {
                if (Error != null)
                {
                    Error(ex);
                }
                else
                {
                    throw;
                }
            }
        };
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜