如何让实例到Dispatcher来停止一个线程?

时间:2018-05-27 10:00:50

标签: c# multithreading

为了逃避线程关联,我使用下面的代码来更新UI控件

public void Start()
{
 while (true)
 {
 App.Current.Dispatcher.Invoke((Action)delegate 
 {
    Messenger.Default.Send(DateTime.Now + "  sent", "SimulatorLogs");
 });
 Thread.Sleep(1000);
 }
}

现在,我希望根据用户操作停止此while循环。可以通过单击GUI中的“停止”按钮。

那么,如何访问此线程以停止循环?

 public void Stop()
 {
       App.Current.Dispatcher.BeginInvoke(new Action(() => App.Current.Shutdown()));
        //This is shutting down entire application :(
 }

0 个答案:

没有答案