开发者

How to stop DispatcherTimer from inside Try - Catch?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜