如何将布尔值从 Worker_ProgressChanged 传回 Worker_DoWork

How to pass booleon back from Worker_ProgressChanged to Worker_DoWork

我正在使用后台工作程序读取值并将值传递给 Worker_ProgressChanged,以更新 UI。

在Worker_DoWork中:

while (agi.DvmReadyToRead)   // wait for digipot to be adjusted before reading in worker
{
    Thread.Sleep(20);
    Application.DoEvents();
    //logS.Debug("Waiting for ready to read in worker");
}
Thread.Sleep(40);  // Give digipot chance to make the change
agi.SendSoftwareTriggerOne();
Thread.Sleep(7);    // Duration for above command to execute
A = agi.ReadOne();
Thread.Sleep(1);    
agi.InitOne();
Thread.Sleep(1);    
sAndH3 = A[0];
worker.ReportProgress(0, new System.Tuple<double>(sAndH3));
agi.DvmReadyToRead = true;

在Worker_ProgressChanged中:

while (!agi.DvmReadyToRead)
{
    //logS.Debug("waiting for ready to read in progress");
    Thread.Sleep(0);
    Thread.Sleep(0);
    Thread.Sleep(0);
    Thread.Sleep(0);
    Thread.Sleep(0);
    Application.DoEvents();  // Exception thown here
    Thread.Sleep(1);     // wait for DVM reading
}
agi.DvmReadyToRead = false;

// Then goes on to adjust output voltage up or down

第一次使用

时效果很好
Application.DoEvents();

但是在第一次 运行 之后,此时我得到了计算器溢出。在阅读了这里的许多帖子后,DoEvents 并不是实现我想要实现的目标的最佳方式。 所以我想要的是一种将布尔值传递回 DoWork 的方法,或者另一种允许工作人员能够读取 agi.DvmReadyToRead 布尔值的方法。

谢谢!

如果我理解您的问题,那么您描述的是测试和测量中的一种非常常见的模式,您的仪器在触发它之后需要一些时间才能获得读数。但是您想知道读取发生的时间,以便您可以采取一些行动(比如更新 ProgressBar 或 TextBox)并且您希望能够取消工作循环。

当我需要自己做这件事时,我喜欢使用 System.Threading.Tasks 来简化事情。我将在这里 post 一个完整的模式,希望您能找到一些有用的东西来解决您遇到的问题。

需要说明的是,我试图回答您关于 "a way to pass a Boolean back to DoWork..." 的问题,方法是从 Worker_DoWork 触发一个可以包含布尔值的事件(如您所问)或双倍(在我的示例中)或您选择的任何其他信息。

祝你好运!

using System;
using System.ComponentModel;    
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Whosebug02
{
    public partial class DVMLoopRunner : Form
    {
        public DVMLoopRunner()
        {
            InitializeComponent();
            DVMReadingAvailable += Form1_DVMReadingAvailable;
            ContinueOrCancel += Form1_ContinueOrCancel;
        }

        // See if User has turned off the Run button then cancel worker
        private void Form1_ContinueOrCancel(Object sender, CancelEventArgs e)
        {
            e.Cancel = !checkBoxRunMeterLoop.Checked;
        }

        // The DVM, after being triggered + some delay, has come up with a new reading.
        private void Form1_DVMReadingAvailable(Object sender, DVMReadingAvailableEventArgs e)
        {
            // To update GUI from worker thread requires Invoke to prevent Cross-Thread Exception
            Invoke((MethodInvoker)delegate
            {
                textBox1.Text = e.Reading.ToString("F4");
            });
        }

        // Make our events so that we can be notified of things that occur
        public event CancelEventHandler ContinueOrCancel;                   
        public event DVMReadingAvailableEventHandler DVMReadingAvailable;

        // This is how we will provide info to the GUI about the new reading
        public delegate void DVMReadingAvailableEventHandler(Object sender, DVMReadingAvailableEventArgs e);
        public class DVMReadingAvailableEventArgs : EventArgs
        {
            public readonly double Reading;
            public DVMReadingAvailableEventArgs(double reading)
            {
                Reading = reading;
            }
        }

        // When the User checks the box, Run the worker loop
        private void checkBoxRunMeterLoop_CheckedChanged(Object sender, EventArgs e)
        {
            if(checkBoxRunMeterLoop.Checked)
            {
                Task.Run(() => ReadDVMWorker());
            }
        }

        // Worker Loop
        private void ReadDVMWorker()
        {
            while(true)
            {
                CancelEventArgs e = new CancelEventArgs();
                ContinueOrCancel?.Invoke(this, e);
                if (e.Cancel) return;               // If User has turned off the Run button then stop worker
                ReadDVM();                          // This worker thread will block on this. So trigger, wait, etc.
            }
        }

        // DVM Takes some period of time after trigger
        void ReadDVM()
        {
            Thread.Sleep(1000);
            double newSimulatedReading = 4.5 + Random.NextDouble();
            DVMReadingAvailable?.Invoke(this, new DVMReadingAvailableEventArgs(newSimulatedReading));
        }
        Random Random = new Random();   // Generate random readings for simulation
    }
}