本文介绍了调用后台工作人员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Invoke()通过后台工作人员执行耗时的功能时显示另一种形式来获取一些用户输入.


I am using Invoke() to get some user input by showing another form while executing a time consuming function by a background worker.


//funcation called inside DoWork of a backgroundworker
public bool ShowSupportLogon()
{
    bool erResult = false;
    System.Threading.Thread.Sleep(5000);//Time-consuming tasks
    //passing control to foreground/main thread to show the form
    this.Invoke((MethodInvoker)delegate()
    {
        DialogResult dr = er.ShowDialog();//er is the object of another form(global variable)
        if (dr == DialogResult.OK)
        {
            erResult = true;
        }
        else
        {
            erResult = false;
        }
    });
    System.Threading.Thread.Sleep(2000);//Time-consuming tasks
    return erResult;
}



我了解到``this.Invoke((MethodInvoker)delegate()''内的代码块是在主线程中执行的,但我的问题是,后台工作程序是否有可能在此代码块完成之前继续执行.



I understand that the block inside ''this.Invoke((MethodInvoker)delegate()'' is executed in the main thread but my question, is there any chance that background worker continue execution before the completion of this block.
Is there anything else that I should be worried about?

推荐答案


这篇关于调用后台工作人员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-29 05:47