views:

457

answers:

3

I have created a GUI (winforms) and added a backgroundworker to run in a separate thread. The backgroundworker needs to update 2 labels continuously. The backgroundworker thread should start with button1 click and run 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 above code just updated the GUI when backgroundworker is done with his job, and that's not what I'm looking for.

+6  A: 

You need to look at BackgroundWorker.ReportProgess.

You can use it to periodically pass back an object to a method in the main thread, which can update the labels for you.

Blorgbeard
A: 
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++;
       }
    }

This works, but it is the correct way of doing it?

Qrew
No, have a look at Blorgbeard's answer
tanascius
A: 

You can use a System.Threading.Timer and update the UI from the Timer's callback method by calling BeginInvoke on the main Form.

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
}
SpaceghostAli