-1

我有一个有两个System.Timers.Timer对象的类。我现在看到的是,一个抛出的异常被另一个捕获。这是正确的行为吗?

class MyClass {
    System.Timers.Timer tOne;
    System.Timers.Timer tTwo;

    tOne.Elapsed += new System.Timers.ElapsedEventHandler(one);
    tTwo.Elapsed += new System.Timers.ElapsedEventHandler(two);

    void one(object sender, System.Timers.ElapsedEventArgs e) {
        try {
             .. some code that throws ...
        } catch (Exception) {
               // not caught here
        }
    }

    void two(object sender, System.Timers.ElapsedEventArgs e) {
        try {
        } catch (Exception) {
               // but caught here
        }
    }
}
4

1 回答 1

1

不同的计时器,但ticks异常被抛出异常后的第一个计时器捕获。您的问题主要是因为您使用的是单线程。

System.Timers.Timer将默默吞下异常并继续计时器(尽管这可能会在框架的未来版本中发生变化);System.Threading.Timer将终止程序。

于 2016-01-29T10:38:05.437 回答