控件'从创建它的线程以外的线程访问。 [英] Control 'accessed from a thread other than the thread it was created on.

查看:117
本文介绍了控件'从创建它的线程以外的线程访问。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有一个要求通知用户有待处理的应用程序

所以在mdiParent中我设置了一个后台工作程序,它继续查询数据库以获取任何待处理的应用程序,如果找到任何显示在MdiParent的工具提示上









In my Application I had A requirement of Notifying the user about the Pending applications
So in the mdiParent I set a Background worker which keeps quering the database to get any pending application and if it finds any display it on the Tooltip on the MdiParent




private void button1_Click(object sender, EventArgs e)
           {
               backgroundWorker1.RunWorkerAsync(2000);
           }
      private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
           {
               fillnotification();
           }


           public void fillnotification()
           {
               int pending = 0;
               if( Program.USERPK!=0){

                   DataTable dt = nftrans.getnotification();
                   pending = dt.Rows.Count;

                   String message = "You Have  " + pending + " Applications Pending For Approval";

                //   toolTip1.SetToolTip(lblStatus , message);
                  toolTip1.Show(message , this, lblStatus.Location);




               }
           }





但是当我运行解决方案时我得到一个例外



跨线程操作无效:控制''MainForm''从一个以外的线程访问它创建的线程。

我理解它由于两个不同的线程但不能排除这个可以任何人建议帮助。我试过我在相关问题中读到的想法但是找不到正确的解决方案



but when Iam running the solution I am getting an exception

Cross-thread operation not valid: Control ''MainForm'' accessed from a thread other than the thread it was created on.
I understood its due to two different thread but Cannot sort this out can any one suggest a help .I tried the ideas I read in related questions But cannot find a correct solution

推荐答案

您只能从创建它们的线程 - UI线程访问控件。试图从另一个线程(例如你正在使用的BackgroundWorker)这样做会给你一个错误。



有两种方法可以做到这一点:

1)你可以使用Invoke将访问权限移回UI线程。

2)你可以告诉主线程它有信息。



就个人而言,我会通过BackgroundWorker Progress报告机制做第二个:

将WorkerReportsProgress属性设置为true。

处理worker ProgressChanged事件。

在处理程序中,获取ProgressChangedEventArgs.ProgressPercentage值并设置工具提示。

在您的worker方法中,通过调用<$ c $替换工具提示代码c> worker.ReportProgress(pending)代替。
You can only access Controls from the thread they were created on - the UI thread. Attempting to do so from another thread (such as the BackgroundWorker you are using) will give you an error.

There are two ways to do this:
1) You could use Invoke to move the access back to the UI thread.
2) You could tell the main thread it has info.

Personally, I would do the second, via the BackgroundWorker Progress reporting mechanism:
Set the WorkerReportsProgress property to true.
Handle the worker ProgressChanged event.
In the handler, get the ProgressChangedEventArgs.ProgressPercentage value and set your tool tip.
In your worker method, replace the tooltip code with a call to the worker.ReportProgress(pending) instead.


这篇关于控件'从创建它的线程以外的线程访问。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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