C# 如何在事件发生时停止调度程序?
我需要在事件第一次勾选后停止连接到调度程序的事件。 你知道怎么做吗C# 如何在事件发生时停止调度程序?,c#,.net,wpf,winforms,C#,.net,Wpf,Winforms,我需要在事件第一次勾选后停止连接到调度程序的事件。 你知道怎么做吗 int closeSeconds = Convert.ToInt32(utility.GetConfiguration("device", "closePopupPrinterAfterSeconds")); var dispatcherTimer = new System.Windows.Threading.DispatcherTimer(); dispatcherTimer.T
int closeSeconds = Convert.ToInt32(utility.GetConfiguration("device", "closePopupPrinterAfterSeconds"));
var dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimerCheckPopupPrinter_Tick);
dispatcherTimer.Interval = new TimeSpan(0, 0, closeSeconds);
dispatcherTimer.Start();
private void dispatcherTimerCheckPopupPrinter_Tick(object sender, EventArgs e)
{
// stop the dispatcherTimer here, so this method will not fire every x seconds
System.Windows.Threading.Dispatcher displatcher = (System.Windows.Threading.Dispatcher)sender;
}
将
sender
参数强制转换为dispatchermer
,而不是Dispatcher
,并调用其Stop
方法,或将其IsEnabled
属性设置为false
private void dispatcherTimerCheckPopupPrinter_Tick(object sender, EventArgs e)
{
var timer = (DispatcherTimer)sender; // not Dispatcher!
timer.Stop(); // or timer.IsEnabled = false;
}
你不能使用dispatchertimer.Tick-=neweventhandler(dispatcherTimerCheckPopupPrinter_Tick)分离处理程序吗?或者只需停止计时器并使用stop()函数?您可以向我展示扩展的代码吗?将Dispatchermer变量设置为全局变量,并在tick事件中使用主题:您可以使用
TimeSpan.FromSeconds(closeSeconds)
的stop方法->Dispatchermer.stop()。然而,这只是我个人的偏好,但我认为这看起来更清楚。