I am using UI with multiple buttons and I need to start background task on button click and report back to main thread + update UI when the task is finished.
This is currently part of my code:
private void button1Click(object sender, EventArgs e)
{
button1.Text = "Starting";
button1.Enabled = false;
button1Worker.RunWorkerAsync();
}
private void button1worker_DoWork(object sender, DoWorkEventArgs e)
{
toolStarter.startTool("button1");
}
private void button1worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
button1.Text = "Autoruns";
button1.Enabled = true;
}
This solution works as intended / expected, however the same code is repeated for 15 different buttons and that seem to be wrong to me.
Can you recommend some other way to do the same thing?
I did try ThreadPool
queue but did not manage to update UI after task finished.