C# 后台工作线程,winform.“跨线程操作无效:”

本文关键字:线程 无效 操作 winform 后台 工作 | 更新日期: 2023-09-27 18:37:22

我正在尝试使用Windows表单使后台工作线程以最基本的方式运行,例如获取后台进程以更改标签中的文本。我在这里得到了基本的后台工作线程代码。http://www.albahari.com/threading/part3.aspx这是我表单中的代码。尝试使它,以便您按下一个按钮,然后生成后台工作线程,这会更改标签中的文本

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
       namespace WindowsFormsApplication4
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            BackgroundWorker _bw = new BackgroundWorker();
             void backgroundio()
            {
                _bw.DoWork += bw_DoWork;
                _bw.RunWorkerAsync("Message to worker");
            }
             void bw_DoWork(object sender, DoWorkEventArgs e)
            {
                // This is called on the worker thread
                label1.Text = (string)(e.Argument);        // writes "Message to worker"
                // Perform time-consuming task...
            }
             void button1_Click(object sender, EventArgs e)
             {
                 backgroundio();
             }
        }
    }

对于标签1。文本 = (字符串)(e.参数);我收到此错误。

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

感谢您的任何帮助! :)

实际上,当我在这里时,有人可以解释这句话吗?

 _bw.DoWork += bw_DoWork;

我不明白 += 在这种情况下有什么意义。 你怎么能添加这些东西?

C# 后台工作线程,winform.“跨线程操作无效:”

Q1

您的bw_doWork方法是静态的。这意味着对于类的所有实例,只有其中一种方法。 该方法无法访问特定于实例的属性、字段或方法。 这解释了编译器错误。

如果将该方法更改为非静态,它将允许您在其中引用label1


问题 2.

引用的语法是向该事件添加事件处理程序的快捷方式。

它只是意味着"将此处理程序添加到给定事件的处理程序列表中"。 这样做的长手方法是使用AddEventHandler。

http://msdn.microsoft.com/en-us/library/system.reflection.eventinfo.addeventhandler.aspx

第三季度

您在运行时收到的神秘消息指示您无法更新非 uI 线程上的 UI 对象。 (bg worker 暗示了不同的线程。 解决方案是在 UI 线程上执行所需的更新。

void bw_DoWork(object sender, DoWorkEventArgs e)
{
    // This is called on the worker thread
    UpdateLabel((string)e.Argument));
      ...more work here...
}
void UpdateLabel(string s)
{
    if (this.label1.InvokeRequired)
    {
        // It's on a different thread, so use Invoke.
        this.BeginInvoke (new MethodInvoker(() => UpdateLabel(s));
    }
    else
    {
        // It's on the same thread, no need for Invoke
        this.label1.Text = s;
    }
}

要了解更多信息,请 http://msdn.microsoft.com/en-us/library/ms171728(v=vs.90).aspx

在DoWork事件中,您应该在不同的线程上执行要执行的工作,即。"不会冻结应用程序的后台工作"。

然后你有一个进度事件(我认为,至少类似的东西) - 这是你更新 GUI 的地方,即更改文本或标签。不要在这里做任何繁重的工作,这是你的主线。

在你的DoWork事件中,即后台线程,你可以使用BackgroundWorker 对象上的方法向主线程报告进度(我不记得方法的名称,比如Report,Progress之类的东西),然后Progress事件将在主线程上调用。

不能从事件更新DoWork UI 控件。UI 可以从 _bw_RunWorkerCompleted 更新。

将要在 UI 元素中更新的值从事件传递到RunWorkerCompleted 甚至通过事件参数DoWork并更新 已完成 事件中的标签。