C#在backgroundworker dowork事件中获取Textbox值

use*_*944 4 c# multithreading backgroundworker thread-safety winforms

在我的Windows窗体应用程序中,我有一个textboxbackgroundworker组件.在dowork的情况下,backgroundworker我试图访问文本框的值.我怎样才能做到这一点?当我尝试访问文本框的值时,我在dowork事件处理程序代码中遇到异常:

Cross-thread operation not valid: Control 'txtFolderName' accessed from a thread other than the thread it was created on`
Run Code Online (Sandbox Code Playgroud)

Adi*_*dil 6

你只能textbox / form controlsGUI线程中访问,你可以这样做.

if(txtFolderName.InvokeRequired)
{
    txtFolderName.Invoke(new MethodInvoker(delegate { name = txtFolderName.text; }));
}
Run Code Online (Sandbox Code Playgroud)