跨线程Winforms控件编辑 [英] Cross-thread Winforms control editing

查看:26
本文介绍了跨线程Winforms控件编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果编辑文本的代码属于"与包含 Windows 窗体的线程不同的线程,我如何编辑 Windows 窗体元素中的文本?我得到了例外:

how can I edit the text in a windows form element if the code that is editing the text 'belongs' to a seperate thread from the one that contains the windows form? I get the exception:

跨线程操作无效:控制textBox1"从创建它的线程以外的线程访问.

Cross-thread operation not valid: Control 'textBox1' accessed from a thread other than the thread it was created on.

谢谢.

推荐答案

您将需要使用 Control.Invoke 方法如下:

You will need to use Control.Invoke method like this:

textbox1.Invoke((MethodInvoker)(() =>
   {
     textbox1.Text="some text";
   }));

也请查看这篇文章:UI 中的线程

这篇关于跨线程Winforms控件编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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