这是我的代码。
BackgroundWorker exportWorker = new BackgroundWorker();
private void btnOK_Click(object sender, RoutedEventArgs e)
{
exportWorker.DoWork += new DoWorkEventHandler(ExportWorkerDoWork);
exportWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(ExportWorkerRunWorkerCompleted);
exportWorker.RunWorkerAsync();
}
void ExportWorkerDoWork(object sender, DoWorkEventArgs e)
{
MethodToPerformInThisThread();
**Dispatcher.Invoke(new Action(() => {MethodofAnotherThreadThatChangesUIStuff();}**
}
void ExportWorkerRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
}
- 当我第一次单击 OK 按钮 (
btnOK_Click
) 时,它会继续运行一切正常。 - 现在,如果我在代码执行完成后第二次单击“确定”按钮,粗体中的代码会运行两次,这意味着该方法
MethodofAnotherThreadThatChangesUIStuff();
会连续调用两次。 - 同样,如果我第三次单击“确定”按钮,
MethodofAnotherThreadThatChangesUIStuff();
则会连续调用 3 次。 - 等等。
我希望MethodofAnotherThreadThatChangesUIStuff();
只调用一次,无论它是哪个点击。我的意思是,一般来说事情应该是这样的。
我在这里想念什么?
任何帮助将不胜感激。