Hi,
I wonder if the below code I have take up "2 threads" on the processor.
In the Form1_Load event I call "startProcess". That process starts a backgroundworker
in a separate class. The process also monitor callbacks from the backgroundworker.
This is what makes me wonder if this takes up "2 threads" on the processor.
The big concern with my question is that I will launch the application 20 times which would
mean that 40 threads are running where perheps 20 of those are not nessecary.
To add is that is a big task to move the backgroundworker to the Form1.cs as the Instance.cs
contain 20,000 lines and alot of code to report back to "progress =>".
So I wonder if it takes up 2 threads. If so can there be a solution to make it only run the backgroundworker
as it is setup now and still monitor "progress =>" ?
//Form1.cs
private void Form1_Load(object sender, EventArgs e)
{
startProcess();
}
//Start thread
void startProcess()
{
//Start the bgWorker with below code
Project.Instance ts = new Project.Instance();
ts.doSomething(
//Here we will update the progress after different scenarios depending on the string that is received(message):
progress =>
{
//Get the returning string here:
String returnString = progress;
MessageBox.Show(returnString);
},
result =>
{
//bgWorker is finished
}
);
}
//Instance.cs
public void doSomething(Action<String> progressCallBack, Action<String> completionCallBack)
{
BackgroundWorker bgWorker = new BackgroundWorker();
bgWorker.WorkerSupportsCancellation = true;
bgWorker.WorkerReportsProgress = true;
bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
bgWorker.ProgressChanged += (sender, args) => { progressCallBack(args.UserState.ToString()); };
bgWorker.RunWorkerCompleted += (sender, args) => { completionCallBack(args.Result.ToString()); };
//Start worker
if (bgWorker.IsBusy == false)
{
bgWorker.RunWorkerAsync();
}
}
private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = (BackgroundWorker)sender;
//This will report back to 'progress'
worker.ReportProgress(10, "Report back something to 'progress'");
//Finished
e.Result = "Process is finished";
}