简体   繁体   English

C#:连续两次调用方法

[英]C# : Method is being called twice in a row

This is my code. 这是我的代码。

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)
{
}
  • When I click on the OK Button ( btnOK_Click ) the first time, it goes ahead and runs everything just fine. 当我第一次单击OK按钮( btnOK_Click )时,它继续运行并运行一切就好了。
  • Now, if I click on the OK Button the second time after the code has finished executing, the code in the bold runs twice, meaning the method MethodofAnotherThreadThatChangesUIStuff(); 现在,如果我在代码完成执行后第二次单击OK按钮,则粗体中的代码运行两次,这意味着方法MethodofAnotherThreadThatChangesUIStuff(); gets called twice in a row. 连续两次被调用。
  • Again, If I click the OK Button the third time, MethodofAnotherThreadThatChangesUIStuff(); 再次,如果我第三次单击OK按钮, MethodofAnotherThreadThatChangesUIStuff(); gets called three times in a row. 连续三次被召唤。
  • and so on. 等等。

I want the MethodofAnotherThreadThatChangesUIStuff(); 我想要MethodofAnotherThreadThatChangesUIStuff(); to be called just once, no matter which click it is. 只需要点击一次即可调用一次。 I mean, generally that's how things should work. 我的意思是,通常这就是事情的运作方式。

What am I missing here? 我在这里错过了什么?

Any help will be really appreciated. 任何帮助将非常感激。

Since you are declaring the background worked outside of the Click handler each time you press the button you add the event handlers again. 由于每次按下按钮时都声明背景在Click处理程序之外工作,因此再次添加事件处理程序。 This is why the second time your method is called twice and the third time it will be called three times... 这就是为什么第二次调用你的方法两次,第三次调用它三次...

To solve this, add the event handlers outside the click method (in the constructor for example) and only leave RunAsync() call in the click method. 要解决此问题,请在click方法之外添加事件处理程序(例如在构造函数中),并在click方法中保留RunAsync()调用。

Each time you click OK, you are creating new event handlers which will all call your DoWork methods. 每次单击“确定”,您都将创建新的事件处理程序,这些处理程序将调用您的DoWork方法。

Place this code in the initialization of your application: 将此代码放在应用程序的初始化中:

exportWorker.DoWork += new DoWorkEventHandler(ExportWorkerDoWork);
exportWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(ExportWorkerRunWorkerCompleted);

Only include the following in your btnOK_Click method: 仅在btnOK_Click方法中包含以下内容:

exportWorker.RunWorkerAsync();

This is being caused because this line: 这是因为这条线:

exportWorker.DoWork += new DoWorkEventHandler(ExportWorkerDoWork);

is being run inside the the btnOK_Click handler. 正在btnOK_Click处理程序中运行。 If you setup the events outside the handler like this: 如果您在处理程序之外设置事件,如下所示:

BackgroundWorker exportWorker = new BackgroundWorker();
exportWorker.DoWork += new DoWorkEventHandler(ExportWorkerDoWork);
exportWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(ExportWorkerRunWorkerCompleted);

private void btnOK_Click(object sender, RoutedEventArgs e)
{
exportWorker.RunWorkerAsync();
}

then everything should work like you expect. 那么一切都应该像你期望的那样工作

Another option would actually be to create a new BackgroundWorker inside the btnOK_Click handler. 另一种选择实际上是在btnOK_Click处理程序中创建一个新的BackgroundWorker Essentially, you would be creating a new worker for each click, setting it up with handlers, and then running it. 基本上,您将为每次单击创建一个新工作程序,使用处理程序进行设置,然后运行它。 You would then discard it and create a new one for each click. 然后,您将丢弃它并为每次点击创建一个新的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM