主用户界面窗口未更新控件-跨线程操作无效 [英] Main UI windows not updating control -Cross-thread operation not valid
问题描述
好..这是问题所在
我有一个主要的UI窗体,该窗体具有一个控件容器,可以向其中添加一些按钮项,也有一个启动列表器的backgroundworker对象.当列表事件触发时,我想在主UI窗体上的该控件容器中创建一个按钮.在我尝试向该容器中添加新控件之前,一切似乎都可以正常工作.我收到以下异常
I have a main UI form that has a control container that i can add some buttons item to it,and also i have a backgroundworker object that starts up a listner. When the listner events fire, i would like to create a button in that control container on the main UI form. Everything seems to work fine until i try to add a new control item to that container. I get the following exception
跨线程操作无效:控制'RadMagnifier_AcceptReject'从创建该线程的线程之外的其他线程访问."
"Cross-thread operation not valid: Control 'RadMagnifier_AcceptReject' accessed from a thread other than the thread it was created on."
代码像这样
Private Sub Mainform_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.SessionTableAdapter.Fill(Me.BCSSDataSet1.Session)
FormatColumns()
Me.BackgroundWorker2.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker2_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker2.DoWork
Notifications()
End Sub
Private Sub Notifications()
'Start listing for events when event is fired try to add a button to a controls container on the UI thread, and that when i get the problem
End Sub
推荐答案
假设您将所有UI操作都移到RunWorkerCompleted方法中,则它看起来像个错误:
Assuming you moved all UI operations into the RunWorkerCompleted method, it looks like a bug:
http://connect.microsoft.com/VisualStudio/feedback /ViewFeedback.aspx?FeedbackID=116930 http://thedatafarm.com/devlifeblog/archive/2005/12/21/39532.aspx
我建议使用防弹(伪代码):
I suggest using the bullet-proof (pseudocode):
if(control.InvokeRequired)
control.Invoke(Action);
else
Action()
这篇关于主用户界面窗口未更新控件-跨线程操作无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!