System.Timers.Timer 已用库中的异常处理



我想在我的WPF应用程序中处理发生的System.Timers.Timer elapsed异常(在我的DLL库中)。但我做不到。它抛出我的DLL库,应用程序将崩溃...有人知道我该如何解决问题吗?

这是我的代码:

public partial class MainWindow : Window
{
    MyClass _myClassInstance = null;
    public MainWindow()
    {
        InitializeComponent();
        try
        {
            _myClassInstance = new MyClass();
        } 
        catch(Exception ex)
        {
            //Here i would like to receive the exception
            //But it never goes in there
            MessageBox.Show(ex.Message);
        }
    }
}
public class MyClass
{
    private System.Timers.Timer _timer = null;
    public MyClass()
    {
        _timer = new Timer();
        _timer.Interval = 2000; //2 Seconds
        _timer.Elapsed += new ElapsedEventHandler(_timer_Elapsed);
        _timer.Start();
        ConnectTo();
    }
    void _timer_Elapsed(object sender, ElapsedEventArgs e)
    {
        //If timer is elapsed I have to raise an exception
        throw new Exception("It's taking longer than expected. Progress cancelled!");
    }
    private void ConnectTo()
    {
        //Just an example implementation
        //Do something!!
        //Connect to SerialPort and wait for correct response
        //If connected than
        _timer.Stop();
    }
}

异常在另一个线程上引发(根据您对 Timing.Timer 的选择)。

在 1 个程序集中尝试这个:你也无法抓住它。它在 DLL 中并不重要。

您只能通过重新思考问题并选择另一种解决方案来解决这个问题。

异常发生在事件内部。这是在另一个线程上运行的,因此它永远不会返回到原始线程。

以不同的方式执行此操作的两种可能性。

  1. 您的串行端口com库具有某种超时功能(也许),只需使用它即可。
  2. 在单独的胎面上检查串行端口。如果你的时间用完了,那就杀死那个线程。

    public class MyClass
    {
        private System.Timers.Timer _timer = null;
        private Thread t;
        public MyClass()
        {
            _timer = new Timer();
            _timer.Interval = 2000; //2 Seconds
            _timer.Elapsed += new ElapsedEventHandler(_timer_Elapsed);
            _timer.Start();
            t = new Thread(new ThreadStart(ConnectTo));
            t.Start();
            t.Join();
        }
        void _timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            //If timer is elapsed I have to raise an exception
            if (t != null)
                t.Abort();
        }
        private void ConnectTo()
        {
            //Just an example implementation
            //Do something!!
            //Connect to SerialPort and wait for correct response
            //If connected than
            _timer.Stop();
        }
    }
    

作为另一种方法,您可以改用事件来代替事件,而不是尝试使用异常来控制您的应用程序流。

public partial class MainWindow : Window
{
    MyClass _myClassInstance = null;
    public MainWindow()
    {
        InitializeComponent();
        _myClassInstance = new MyClass();
        _myClassInstance.TimedOut += delegate (object sender, EventArgs e) {
            ((MyClass)sender).CancelConnect();
            MessageBox.Show("Timeout!");
        };
        _myClassInstance.ConnectTo();
    }
}
...
public class MyClass
{
    Timer _timer = new Timer();
    public event EventHandler TimedOut;
    void _timer_Elapsed(object sender, ElapsedEventArgs e)
    {
        OnTimedOut();
    }
    private void OnTimedOut()
    {
        var handler = TimedOut;
        if (handler != null)
        {
            handler(this, EventArgs.Empty);
        }
    }
    public void ConnectTo(int timeout = 2000)
    {
        CancelConnect();
        _timer.Interval = timeout; // pass timeout in so it's flexible
        _timer.Elapsed += new ElapsedEventHandler(_timer_Elapsed);
        _timer.Start();
        // do connect stuff...
        _timer.Stop();
    }
    public void CancelConnect()
    {
        _timer.Stop();
        // cancel connect stuff...
    }
}

我认为您的构造函数中有太多的事情要做MyClass所以我将其移动到您直接从MainWindow调用的ConnectTo中。

不起作用:

MessageBox.Show(e.Message); doen's throw

public class MyClass
{
    private System.Timers.Timer _timer = null;
    private Thread t;
    public MyClass()
    {
        try
        {
        _timer = new System.Timers.Timer();
        _timer.Interval = 5000; //2 Seconds
        _timer.Elapsed += new ElapsedEventHandler(_timer_Elapsed);
        _timer.Start();
        t = new Thread(new ThreadStart(ConnectTo));
        t.Start();
        t.Join();
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
        }
    }
    void _timer_Elapsed(object sender, ElapsedEventArgs e)
    {
        //If timer is elapsed I have to raise an exception
        if (t != null)
        {
            t.Abort();
        }
    }
    private void ConnectTo()
    {
        //Just an example implementation
        //Do something!!
        try
        {
            //Connect to SerialPort and wait for correct response
            using (SqlConnection _SC = new SqlConnection("aaaa"))
            {
                _SC.Open();
            }
        }
        catch (Exception)
        {
            throw;
        }
        finally
        {
            //If connected than
            _timer.Stop();
        }

    }
}

最新更新