How to handle exception using Timer (Thread) class

筅森魡賤 提交于 2019-12-24 05:38:47

问题


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!");
 }
}

回答1:


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;
                }
            }
        };
    }
}


来源:https://stackoverflow.com/questions/5406904/how-to-handle-exception-using-timer-thread-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!