Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从内部阻止调度员尝试捕获?_C#_Wpf_Timer_Try Catch_Dispatcher - Fatal编程技术网

C# 如何从内部阻止调度员尝试捕获?

C# 如何从内部阻止调度员尝试捕获?,c#,wpf,timer,try-catch,dispatcher,C#,Wpf,Timer,Try Catch,Dispatcher,请帮助我了解如何停止在Dispatchermer中执行MethodOne()的尝试。在第一次尝试执行失败后,请勾选WPF Dispatchermer的事件处理程序 TimeSpan ts = new TimeSpan(0, 0, 5); DispatcherTimer dispatcherTimer = new DispatcherTimer(); dispatcherTimer.Tick += new EventHandler(dispatche

请帮助我了解如何停止在
Dispatchermer中执行
MethodOne()
的尝试。在第一次尝试执行失败后,请勾选WPF Dispatchermer的事件处理程序

        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
        {
           MethodOne()
        }
        catch (Exception)
        {
            // Here I would like prevent code from trying to execute MethodOne()
        }  
    }
我想设置一些锁或停止计时器,但尝试这样做时,我遇到了从Try-Catch结构中查看其他代码的问题,并且不确定如何正确克服它。

这就是“sender”参数的作用:

private void dispatcherTimer_Tick(object sender, EventArgs e)
{           
    try
    {
       MethodOne()
    }
    catch (Exception)
    {
        (sender as DispatcherTimer).Stop();
    }  
}
这就是“发件人”参数的作用:

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