我创建了一个简单的 WPF 应用程序,并向默认窗口添加了一个按钮。当我单击按钮时,会调用一个模拟的长工作方法(使用 Thread.Sleep(15000) 进行模拟)。我试图使按钮异步执行,但是尽管遵循在线示例,按钮和整个窗口都会在我锁定时立即锁定单击并保持该状态直到 Thread.Sleep(...) 完成。
有什么想法为什么会发生这种情况吗?
这是代码:
private void button1_Click(object sender, RoutedEventArgs e)
{
DoSomeAsyncWork();
}
private void DoSomeAsyncWork()
{
System.Windows.Threading.Dispatcher.Run();
Thread thread = new System.Threading.Thread(
new System.Threading.ThreadStart(
delegate()
{
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => Thread.Sleep(15000)));
}
));
thread.Start();
}
您正在将长操作放回到 UI 线程中。让我评论一下你的例子:
Thread thread = new System.Threading.Thread(
new System.Threading.ThreadStart(
delegate() {
// here we are in the background thread
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
new Action(() => {
// here we are back in the UI thread
Thread.Sleep(15000);
}));
}
));
因此,您应该像这样修改您的示例:
Thread thread = new System.Threading.Thread(
new System.Threading.ThreadStart(
delegate() {
// here we are in the background thread
Thread.Sleep(15000); // <-- do the long operation here
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
new Action(() => {
// here we are back in the UI thread
// do stuff here that needs to update the UI after the operation finished
}));
}
));
正如其他人提到的,使用 BackgroundWorker 类更容易。这是一个例子:
private void DoSomeAsyncWork()
{
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += (sender, args) => {
// do your lengthy stuff here -- this will happen in a separate thread
Thread.Sleep(15000);
}
bw.RunWorkerCompleted += (sender, args) => {
if (args.Error != null) // if an exception occurred during DoWork,
MessageBox.Show(args.Error.ToString()); // do your error handling here
// do any UI stuff after the long operation here
...
}
bw.RunWorkerAsync(); // start the background worker
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)