C# 在 backgroundworker dowork 事件中获取文本框值

标签 c# multithreading winforms thread-safety backgroundworker

在我的 Windows 窗体应用程序中,我有一个 textboxbackgroundworker 组件。在 backgroundworkerdowork 事件中,我试图访问文本框的值。我怎样才能做到这一点?当我尝试访问文本框的值时,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/

相关文章:

c# - 形成 Load() 或不形成 Load()

c# - VS 2010 代码可读性提示/插件

Python回车和线程

c - 带约束的迷宫求解

c# - 替换现有图像时崩溃

c# - 如何为 Winforms RichTextBox 设置交替的线条颜色?

c# - 在使用 C# 的 Visual Studio 2008 中,如何设置属性监视?

c# - StructureMap 到 Ninject 的转换

c# - 如何知道一组 BackgroundWorker 何时完成

java - 为什么我的两个线程可以通过非 volatile 字段进行协调?