Hi guys, I'm trying to wrap my mind around this asynchronous and background worker stuff ...
As a playground, i have a WPF application that searches for prime numbers.
Basics are easy. Create bgw (my short for BackGroundWorker), create DoWork, RunWorkerCompleted, ProgressChanged delegates. Once button clicked, run it async. once work is finished, update the GUI. Everybody happy. Here's the main code:
private void btn_no_opt_go_Click(object sender, RoutedEventArgs e)
{
try {
...
bgw.RunWorkerAsync(int.Parse(time_to_run.Text));
} catch { ... }
}
private void bgw_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker bgw = sender as BackgroundWorker;
int arg = (int)e.Argument;
PrimeFinder pf = new PrimeFinder(arg);
e.Result = pf.No_Optimization(bgw);
}
private void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
}
private void bgw_ProgressChanged(bject sender, System.ComponentModel.ProgressChangedEventArgs e)
{
prgs_no_opt.Value = e.ProgressPercentage;
}
private void btn_no_opt_cancel_Click(object sender, RoutedEventArgs e) {...}
There's also a class called Prime Finder which has:
public PrimesResults No_Optimization(BackgroundWorker bgw)
{
...
}
The GUI looks something like this:
No Optimization: [ progress bar ] [Go button] [Cancel button]
Results label: [holds the result / cancel / error code]
This works nicely, and all are happy.
Now i want to improve and add another method that does things better. So, i'm adding to the GUI which now looks like:
No Optimization: [ progress bar ] [Go button] [Cancel button]
Results label: [holds the result / cancel / error code]
Some Optimization: [ progress bar ] [Go button] [Cancel button]
Results label: [holds the result / cancel / error code]
So, i copy all my buttons and labels, paste them, rename them around so they make sense, and then i copy paste most of the above code. instead of bgw i have bgw_second, with listeners for bgw_second_DoWork instead of bgw_DoWork, and so on.
This seems silly, redundant, prone to mistakes, wasteful, and so on.
How could I have different buttons (each button has a matching "cancel" button), call and listen to different methods without copy pasting the above things?
Notice that different buttons call different bgw, which call another class with that bgw so they can report progress to the correct progress bar, and cancel the correct thread
Another question is: if I use 1 bgw, and call it with different parameters so it calls different methods, will it run on 1 thread, or will it spawn as many threads as needed (which is the case if i create 2 or more different bgw and each button calls one of them).
Having a multiple core CPU, hitting the Go button on the first method, will max one core while the process is working (makes sense, lots of calculations).
Hitting the first and then the second Go buttons, will max out 2 cores. Makes sense, since i'll take it them system spans 2 bgw, each gets his core, and they chunk away happily (or the equivalent to a core on different ones. looking at the task manager, i don't see one core being fully loaded, but i see the load is evenly shared (give or take)
So the question is if i use 1 bgw, and give him as an argument (from the button clicked) the method to call, will clicking those 2 buttons result in 2 threads being used (as it happens now), or will only 1 thread be used, sharing the time between the 2 calls.
I wanted to attach the project, but cant find an upload option.
Cheers :)