我怎么能避免invalidOperationException时使用一个后台工作者

本文关键字:一个 工作者 后台 能避免 invalidOperationException | 更新日期: 2023-09-27 18:01:48

我有这个函数:

private void cpuView()
        {
            if (pauseContinueDoWork == true)
            {
            }
            else
            {
                Computer myComputer = new Computer();
                myComputer = new Computer(settings) { CPUEnabled = true };
                myComputer.Open();
                Trace.WriteLine("");
                foreach (var hardwareItem in myComputer.Hardware)
                {
                    if (hardwareItem.HardwareType == HardwareType.CPU)
                    {
                        hardwareItem.Update();
                        foreach (IHardware subHardware in hardwareItem.SubHardware)
                            subHardware.Update();
                        foreach (var sensor in hardwareItem.Sensors)
                        {
                            settings.SetValue("sensor", sensor.Value.ToString());
                            if (sensor.SensorType == SensorType.Temperature)
                            {
                                sensor.Hardware.Update();
                                settings.GetValue("sensor", sensor.Value.ToString());
                                label17.Text = sensor.Value.ToString() + "c";//String.Format("{0} Temperature = {1}c", sensor.Name, sensor.Value.HasValue ? sensor.Value.Value.ToString() : "no value");
                                tempCpuValue = sensor.Value;
                                if (sensor.Value > 60)
                                {
                                    Logger.Write("The Current CPU Temperature Is ===> " + sensor.Value);
                                    button1.Enabled = true;
                                }
                                int t = label17.Text.Length;
                                if (t >= 4)
                                {
                                    label17.Location = new Point(50, 50); // not working to check everything about the locations ''
                                }
                                else
                                {
                                    label17.Location = new Point(50, 50);
                                }
                                break;
                            }
                        }
                    }
                }
            }
        }

我在后台worker (DoWork事件)中调用这个函数:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            while (true)
            {
                if ((worker.CancellationPending == true))
                {
                    e.Cancel = true;
                    break;
                }
                else
                {
                    if (tempCpuValue >= (float?)nud1.Value || tempGpuValue >= (float?)nud1.Value)
                    {
                        soundPlay = true;
                        blinking_label();
                        NudgeMe();
                    }
                    else
                    {
                        soundPlay = false;
                        stop_alarm = true;
                    }
                    cpuView();
                    gpuView();
                    Thread.Sleep(1000);
                }
            }
        }

在这种情况下,异常在这一行的cpuview函数中:

label17.Text = sensor.Value.ToString() + "c";

错误信息是:跨线程操作无效:控制'label17'从创建它的线程以外的线程访问。

异常消息:

System.InvalidOperationException was unhandled by user code
  HResult=-2146233079
  Message=Cross-thread operation not valid: Control 'label17' accessed from a thread other than the thread it was created on.
  Source=System.Windows.Forms
  StackTrace:
       at System.Windows.Forms.Control.get_Handle()
       at System.Windows.Forms.Control.set_WindowText(String value)
       at System.Windows.Forms.Control.set_Text(String value)
       at System.Windows.Forms.Label.set_Text(String value)
       at HardwareMonitoring.Form1.cpuView() in d:'C-Sharp'HardwareMonitoring'HardwareMonitoring'Hardwaremonitoring'Form1.cs:line 378
       at HardwareMonitoring.Form1.backgroundWorker1_DoWork(Object sender, DoWorkEventArgs e) in d:'C-Sharp'HardwareMonitoring'HardwareMonitoring'Hardwaremonitoring'Form1.cs:line 655
       at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
       at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)
  InnerException: 

我该如何解决/修复它?

我怎么能避免invalidOperationException时使用一个后台工作者

你正在做后台线程到UI线程的调用。你需要在控件上使用BeginInvoke来更新它,或者使用ReportProgress方法。

例如:

public delegate void InvokeDelegate();
private void DoWork()
{
    label17.BeginInvoke(new InvokeDelegate(InvokeMethod));
}
public void InvokeMethod()
{
   label17.Text = "I execute on the UI thread!";
}

您正在尝试从后台线程访问UI -因此出现"跨线程操作无效:"消息。

你需要从你的后台工作者发送一个事件到UI线程,并使用Invoke来更新UI。这里我将它实现为一个扩展方法,因此它可以从任何控件调用:

public static void InvokeIfRequired(this Control control, Action action)
{
    if (control.InvokeRequired)
    {
        control.Invoke(action);
    }
    else
    {
        action();
    }
}

其中control是您要更新的UI元素,action()是您要调用的方法。然后你可以这样调用它:

this.label17.InvokeIfRequired(() => this.label17.UpdateLabel(sensor.Value.ToString() + "c"));
private static void UpdateLabel(this Label label, string newValue)
{
    label.Text = newValue;
}