为了逃避线程关联,我使用下面的代码来更新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 :(
}