在我的 Windows 窗体应用程序中,我有一个 textbox
和 backgroundworker
组件。在 backgroundworker
的 dowork
事件中,我试图访问文本框的值。我怎样才能做到这一点?当我尝试访问文本框的值时,dowork 事件处理程序代码出现以下异常:
Cross-thread operation not valid: Control 'txtFolderName' accessed from a thread other than the thread it was created on`
最佳答案
您只能在GUI 线程 中访问文本框/表单控件
,您可以这样做。
if(txtFolderName.InvokeRequired)
{
txtFolderName.Invoke(new MethodInvoker(delegate { name = txtFolderName.text; }));
}
关于C# 在 backgroundworker dowork 事件中获取文本框值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15760654/