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)
{
argumentData.RPM = x;
argumentData.MAP = x * 2;
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.
source
share