1

我想在我的 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();
    }
}
4

4 回答 4

3

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

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

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

于 2012-09-25T12:07:20.997 回答
1

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

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

  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();
        }
    }
    
于 2012-09-25T12:09:01.830 回答
1

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

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所以我将它移到ConnectTo你直接从你的MainWindow.

于 2012-09-25T12:23:18.607 回答
0

不行:

MessageBox.Show(e.Message); 多恩的投掷

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



    }
}
于 2014-06-18T08:33:23.220 回答