如何从backgrowndworker线程提供UI [英] how do I feed UI from backgrowndworker thread

查看:56
本文介绍了如何从backgrowndworker线程提供UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码更新进度条<   pre     lang   =  c# >  private void btnSearch_Click(object sender,EventArgs e)
{


worker.DoWork + = worker_DoWork;
worker.WorkerReportsProgress = true;
worker.ProgressChanged + = worker_ProgressChanged;
worker.RunWorkerAsync();

}

void worker_ProgressChanged(object sender,ProgressChangedEventArgs e)
{
updateUI();
}


void worker_DoWork(对象发送者,DoWorkEventArgs e)
{

myfunction();
Thread.Sleep(100);
worker.ReportProgress(paths.currentProgress);


}


public void updateUI(){
pBar.Minimum = 0;
pBar.Maximum = 100;
pBar.Step = paths.currentProgress; //我从myfunction获得的值()
pBar.PerformStep();
} < / pre > 它不会更新UI(进度条)。

然后我用这种方式尝试

< pre lang = c# > private void btnSearch_Click(object sender,EventArgs e)
{


worker.DoWork + = worker_DoWork;
worker.RunWorkerAsync();

}


void worker_DoWork(对象发送者,DoWorkEventArgs e)
{

myfunction();
Thread.Sleep(100);
updateUI();



}


public void updateUI(){
pBar.Minimum = 0;
pBar.Maximum = 100;
pBar.Step = paths.currentProgress; //我从myfunction获得的值()
pBar.PerformStep();
} < / pre >

这次它给了我错误
< pre > System.Windows.Forms.dll中出现System.InvalidOperationException类型的异常,但未在用户代码中处理

附加信息:跨线程操作无效:控制从创建它的线程以外的线程访问的'pBar'。< / pre >

请帮助我如何从我调用的函数中提供UI在后台工作者。

解决方案

错误是因为您正在访问/修改在另一个线程上运行的UI控件。 />


//添加此行i你的构造函数

Control.CheckForIllegalCrossThreadCalls = false;



但请记住这是不是线程安全的。有关详细信息,请参阅此链接



感谢

I am using following code to update the progress bar <pre lang="c#"> private void btnSearch_Click(object sender, EventArgs e)
        {


            worker.DoWork += worker_DoWork;
            worker.WorkerReportsProgress = true;
            worker.ProgressChanged += worker_ProgressChanged;
            worker.RunWorkerAsync();

        }

        void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            updateUI();
        }


        void worker_DoWork(object sender, DoWorkEventArgs e)
        {

           myfunction();
            Thread.Sleep(100);
            worker.ReportProgress(paths.currentProgress);


        }


        public void updateUI(){
            pBar.Minimum = 0;
            pBar.Maximum = 100;
            pBar.Step = paths.currentProgress; // a value I get from myfunction()
            pBar.PerformStep();
        }</pre>  it do not update the UI(progressbar).

then I tried it this way

<pre lang="c#">private void btnSearch_Click(object sender, EventArgs e)
        {


            worker.DoWork += worker_DoWork;
            worker.RunWorkerAsync();

        }


        void worker_DoWork(object sender, DoWorkEventArgs e)
        {

           myfunction();
            Thread.Sleep(100);
           updateUI();



        }


        public void updateUI(){
            pBar.Minimum = 0;
            pBar.Maximum = 100;
            pBar.Step = paths.currentProgress; // a value I get from myfunction()
            pBar.PerformStep();
        }</pre>

this time it gives me the error
<pre>An exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll but was not handled in user code

Additional information: Cross-thread operation not valid: Control 'pBar' accessed from a thread other than the thread it was created on.</pre>

please help me how do I feed the UI from the function I have called in back ground worker.

解决方案

The error is because you are accessing /modifying the UI control which is running on another thread .

//add this line in your constructor
Control.CheckForIllegalCrossThreadCalls = false;

but remember this is not thread safe . for more details follow this Link

thanks


这篇关于如何从backgrowndworker线程提供UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆