Reputation: 1011
I am totally lost. I just seem to get no response from it.
BackgroundWorker NewWorker;
public void StartBackgroundWorker()
{
BackgroundWorker NewWorker = new BackgroundWorker();
NewWorker.DoWork += new DoWorkEventHandler(NewWorker_DoWork);
NewWorker.ProgressChanged += new ProgressChangedEventHandler(NewWorker_ProgressChanged);
NewWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(NewWorker_RunWorkerCompleted);
NewWorker.WorkerReportsProgress = true;
StartWorker();
}
void NewWorker_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
for (int i = 1; (i <= 10); i++)
{
if ((worker.CancellationPending == true))
{
e.Cancel = true;
break;
}
else
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select StatusCode from Win32_PingStatus where address = 'Metabox-PC'");
ManagementObjectCollection objCollection = searcher.Get();
foreach (ManagementObject Results in objCollection)
{
MessageBox.Show(Results.ToString());
}
// Perform a time consuming operation and report progress.
System.Threading.Thread.Sleep(1);
worker.ReportProgress((i * 10));
}
}
}
private void NewWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show("Finished");
}
None of the message boxes appear, indicating it's finished. What have I missed?
Upvotes: 0
Views: 302
Reputation: 1011
Thank you to all your answers. The description supplied makes more sense to me now.
Also to the previous debate: the message box does block the thread. It got stuck at 10% and then crashed my computer (VM box).
Now my only issue is that e.results
is returning an array of items. But it is in object.
I am finding it difficult to get the data back.
public void StartBackgroundWorker()
{
BackgroundWorker NewWorker = new BackgroundWorker();
NewWorker.DoWork += new DoWorkEventHandler(NewWorker_DoWork);
NewWorker.ProgressChanged += new ProgressChangedEventHandler(NewWorker_ProgressChanged);
NewWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(NewWorker_RunWorkerCompleted);
NewWorker.WorkerReportsProgress = true;
NewWorker.RunWorkerAsync();
}
void NewWorker_DoWork(object sender, DoWorkEventArgs e)
{
List<string> ReturnResults = new List<string>();
BackgroundWorker worker = sender as BackgroundWorker;
ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select StatusCode from Win32_PingStatus where address = 'Metabox-PC'");
ManagementObjectCollection objCollection = searcher.Get();
foreach (ManagementObject Results in objCollection)
{
ReturnResults.Add(Results["StatusCode"].ToString());
}
e.Result = ReturnResults;
// Perform a time-consuming operation and report progress.
System.Threading.Thread.Sleep(1);
}
private void NewWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
txtPingable.text = e.Result;
}
Upvotes: 0
Reputation: 16119
Francis is right... Any attempt to call MessageBox in a thread other than the GUI thread will block. You'll have to invoke:
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => {
MessageBox.Show("Hello World!");
}));
Upvotes: 0
Reputation: 10865
Why do you need to make the background worker a field? Just make it in the constructor and everything else like. In its events you can just cast the object as the BackgroundWorker.
public void StartBackgroundWorker()
{
BackgroundWorker NewWorker = new BackgroundWorker();
NewWorker.DoWork += new DoWorkEventHandler(NewWorker_DoWork);
NewWorker.ProgressChanged += new ProgressChangedEventHandler(NewWorker_ProgressChanged);
NewWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(NewWorker_RunWorkerCompleted);
NewWorker.WorkerReportsProgress = true;
NewWorker.RunWorkerAsync();
}
Upvotes: 1
Reputation: 203804
You're never calling RunWorkerAsync
on the BackgroundWorker
that you have configured.
You think you are, because you're calling StartWorker
which, (I presume) contains the code NewWorker.RunWorkerAsync();
somewhere in it, however NewWorker
, inside of StartWorker
, is referring to an entirely different BackgroundWorker
that you have not configured to do anything.
You have an instance field, NewWorker
, and a local variable NewWorker
inside of StartBackgroundWorker
that is shadowing the instance field.
If you really need the background worker to be an instance field, then don't shadow it in StartBackgroundWorker
and use the instance field throughout. If you don't need it to be an instance field (which, for the record, is likely, and needlessly promoting variables to instance fields makes for messier programs) then you simply need to start the BackgroundWorkder
you created in StartBackgroundWorker
. If StartWorker
really has enough code that it needs to be in another method, then it probably means it should accept a BackgroundWorker
as a parameter, so you can pass in the worker you created in StartBackgroundWorker
.
Upvotes: 2
Reputation: 26268
The problem is that you never start your NewWorker. That's because your "global" NewWorker is always null.
See the fixed code:
NewWorker = new BackgroundWorker(); // this line is now fixed.
NewWorker.DoWork += new DoWorkEventHandler(NewWorker_DoWork);
NewWorker.ProgressChanged += new ProgressChangedEventHandler(NewWorker_ProgressChanged);
NewWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(NewWorker_RunWorkerCompleted);
NewWorker.WorkerReportsProgress = true;
StartWorker();
Upvotes: 2