【发布时间】:2011-03-23 14:38:59
【问题描述】:
我正在尝试处理Timer 的异常。如果该类有类似HandlerExceptionEvent 这样的东西会很好,这样我们就可以添加一些事件来记录某些内容或停止计时器。
PS:我不想在ElapsedEventHandler() 中添加try/catch 块。
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!");
}
}
【问题讨论】:
-
为什么不想在事件处理程序中添加
try-catch? -
因为我知道如何尝试/捕获。 :-) 我想知道我是否可以使用我不知道的东西。
标签: c# multithreading timer handler