根据Prerak K的更新评论(已删除):
我想我没有正确提出问题。
情况是这样的:我想根据控件的值将数据加载到全局变量中。我不想从子线程更改控件的值。我永远不会从子线程执行此操作。
因此,仅访问该值,以便可以从数据库中获取相应的数据。
您想要的解决方案应如下所示:
UserContrl1_LOadDataMethod(){ string name = ""; if(textbox1.InvokeRequired) { textbox1.Invoke(new MethodInvoker(delegate { name = textbox1.text; })); } if(name == "MyName") { // do whatever }}在 尝试切换回控件的线程 之前, 请在单独的线程中 进行 认真的处理。例如:
UserContrl1_LOadDataMethod(){ if(textbox1.text=="MyName") //<<======Now it wont give exception** { //Load data correspondin to "MyName" //Populate a globale variable List<string> which will be //bound to grid at some later stage if(InvokeRequired) { // after we've done all the processing, this.Invoke(new MethodInvoker(delegate { // load the control with the appropriate data })); return; } }}


