C # constantly updates GUI from background worker

I created a graphical interface (winforms) and added a background worker to run in a separate thread. The background worker must constantly update 2 tags. The backgroundworker sheet should start by pressing button1 and start forever.

    class EcuData 
    {
        public int RPM { get; set; }
        public int MAP { get; set; }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        EcuData data = new EcuData
        {
            RPM = 0,
            MAP = 0
        };
        BackWorker1.RunWorkerAsync(data);
    }

    private void BackWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        EcuData argumentData = e.Argument as EcuData;
        int x = 0;
        while (x<=10)
        {
            //
            // Code for reading in data from hardware.
            //
            argumentData.RPM = x;           //x is for testing only!
            argumentData.MAP = x * 2;       //x is for testing only!
            e.Result = argumentData;
            Thread.Sleep(100);
            x++;
       }
    private void BackWorker1_RunWorkerCompleted_1(object sender,  RunWorkerCompletedEventArgs e)
    {
        EcuData data = e.Result as EcuData;
        label1.Text = data.RPM.ToString();
        label2.Text = data.MAP.ToString();
    }
}

The code above just updated the GUI when the background work is done with its work, and that is not what I am looking for.

+3
source share
3 answers

You need to look at BackgroundWorker.ReportProgess .

, , .

+9

System.Threading.Timer Timer, BeginInvoke .

uiUpdateTimer = new System.Threading.Timer(new TimerCallback(
            UpdateUI), null, 200, 200);

private void UpdateUI(object state)
{
    this.BeginInvoke(new MethodInvoker(UpdateUI));
}

private void UpdateUI()
{
    // modify labels here
}
+3
class EcuData
    {
        public int RPM { get; set; }
        public int MAP { get; set; }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        EcuData data = new EcuData
        {
            RPM = 0,
            MAP = 0
        };
        BackWorker1.RunWorkerAsync(data);
    }

    private void BackWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        EcuData argumentData = e.Argument as EcuData;
        int x = 0;
        while (x<=10)
        {
            e.Result = argumentData;
            Thread.Sleep(100);
            this.Invoke((MethodInvoker)delegate
            {
                label1.Text = Convert.ToString(argumentData.RPM = x);           //send hardware data later instead, x is for testing only!
                label2.Text = Convert.ToString(argumentData.MAP = x * 2);       //send hardware data later instead, x is for testing only!
            });
            x++;
       }
    }

It works, but is this the right way to do it?

+1
source

Source: https://habr.com/ru/post/1739277/


All Articles