patterncsharpMinor
Abuse/Misuse of C# BackgroundWorker?
Viewed 0 times
abusebackgroundworkermisuse
Problem
I have finished a program, and it does what I want it to do, but I feel I am "doing it wrong", even though it's seemingly efficient enough. I have prepared a small example of what I feel I am handling wrong with the
First, I have a form with 1 button, 1
Form1.cs
Looping.cs
StripHandler.cs
```
namespace StatusStripTest
{
public class StripHandler
{
public static void UpdateStatus(StatusStrip ss, String Status
backgroundworker class and would like to see if I could have handled this any more cleanly.First, I have a form with 1 button, 1
statusstrip, 1 toolstripprogressbar, and 1 toolstriplabel. I update the statusstrio items while the backgroundworker is running, as well as show messageboxes across classes. See below.Form1.cs
namespace StatusStripTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void prepareToRun()
{
if (!backgroundWorker1.IsBusy)
{
backgroundWorker1 = new BackgroundWorker();
backgroundWorker1.DoWork += delegate
{
Looping.loop(statusStrip1);
};
backgroundWorker1.RunWorkerAsync();
}
}
private void button1_Click(object sender, EventArgs e)
{
prepareToRun();
}
}
}Looping.cs
namespace StatusStripTest
{
public class Looping
{
public static void loop(StatusStrip strip)
{
Form1 Form1 = new Form1();
string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
while (true)
{
MessageBox.Show("Time to loop");
foreach (char c in alphabet)
{
StripHandler.UpdateProgress(strip, alphabet.IndexOf(c), alphabet.Length);
StripHandler.UpdateStatus(strip, c.ToString());
}
}
}
}
}StripHandler.cs
```
namespace StatusStripTest
{
public class StripHandler
{
public static void UpdateStatus(StatusStrip ss, String Status
Solution
In general, passing controls to the
This event should be handled on GUI level so it has access to the controls without passing them down to worker. Draft of solution:
BackgroundWorker should not be necessary at all. Think about it as it simply has some job to do and that's all. However, it can of course report about progress of its job. Those two ideas were taken into account when designing BackgroundWorker class and are reflected by DoWork and ProgressChanged events respectively. There is also a dedicated BackgroundWorker.ReportProgress method which raises ProgressChanged event. This event should be handled on GUI level so it has access to the controls without passing them down to worker. Draft of solution:
private void prepareToRun()
{
if (!backgroundWorker1.IsBusy)
{
backgroundWorker1 = new BackgroundWorker();
backgroundWorker1.DoWork += backgroundWorker_DoWork;
backgroundWorker1.ProgressChanged += backgroundWorker_ProgressChanged;
backgroundWorker1.RunWorkerAsync();
}
}
public static void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
// use worker.ReportProgress(percent, someData) somewhere here
}
private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// Update your statusStrip1 here according to data passed in e.ObjectState
}Code Snippets
private void prepareToRun()
{
if (!backgroundWorker1.IsBusy)
{
backgroundWorker1 = new BackgroundWorker();
backgroundWorker1.DoWork += backgroundWorker_DoWork;
backgroundWorker1.ProgressChanged += backgroundWorker_ProgressChanged;
backgroundWorker1.RunWorkerAsync();
}
}
public static void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
// use worker.ReportProgress(percent, someData) somewhere here
}
private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// Update your statusStrip1 here according to data passed in e.ObjectState
}Context
StackExchange Code Review Q#39733, answer score: 8
Revisions (0)
No revisions yet.