开发者

How to stop DispatcherTimer from inside Try - Catch?

开发者 https://www.devze.com 2022-12-18 02:00 出处:网络
Please, help me to understand how I could stop attempts of executing MethodOne() inside a dispatcherTimer.Tick event handler of WPF DispatcherTimer after first unsuccessful attempt of doing it.

Please, help me to understand how I could stop attempts of executing MethodOne() inside a dispatcherTimer.Tick event handler of WPF DispatcherTimer after first unsuccessful attempt of doing it.

        TimeSpan ts = new TimeSpan(0, 0, 5);
        DispatcherTimer dispatcherTimer = new DispatcherTimer();
        dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
        dispatcherTimer.Interval = ts;
        dispatcherTimer.Start();
 ...

    private void dispatcherTimer_Tick(object sender, EventArgs e)
    {           
        try
        {
         开发者_C百科  MethodOne()
        }
        catch (Exception)
        {
            // Here I would like prevent code from trying to execute MethodOne()
        }  
    }

I would like to set some lock or to stop timer, but trying to do it I faced problems of visibility of other code from inside a Try-Catch construction and not sure how to overcome it correctly.


That's what the "sender" argument is for:

private void dispatcherTimer_Tick(object sender, EventArgs e)
{           
    try
    {
       MethodOne()
    }
    catch (Exception)
    {
        (sender as DispatcherTimer).Stop();
    }  
}
0

精彩评论

暂无评论...
验证码 换一张
取 消