First let me say I am new to Windows apps and .NET threading (and debugging them...). I am writing a Windows app that on a button click runs a WMI query out on the network and returns a list of files to the user that is displayed in a listbox control. I wanted to display a progress bar that would have a marquee effect to to let the user know the app was in effect searching for files.
I was told a good way to do this would be to run the WMI query in a background process using a backgroundworker object so that the UI could remain active and responsive to the user and my progress bar would show with the marquee effect. This would let the user know it was still searching.
However, it does not seem that my bgWorker_DoWork() method is executing. No results are currently returned. Is there a way I can prove or disprove bgWorker_DoWork() is executing. Am still tying to figure out how the "Attach to Process" functionality works in VS. In the main thread the last thing I see happening is bgWorker.RunWorkerAsync(cmbNetworkComputers.SelectedText) being called. Any help or suggestions would be greatly appreciated. Code is below.
private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
listBox1.Items.Clear();
cmbNetworkComputers.Focus();
progressBar1.Visible = true;
progressBar1.Style = ProgressBarStyle.Marquee;
// Set up background worker object & hook up handlers
BackgroundWorker bgWorker;
bgWorker = new BackgroundWorker();
bgWorker.WorkerReportsProgress = true;
bgWorker.ProgressChanged += new ProgressChangedEventHandler(bgWorker_ProgressChanged);
bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgWorker_RunWorkerCompleted);
bgWorker.RunWorkerAsync(cmbNetworkComputers.SelectedText);
}
private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
string strComputer = (string)e.Argument;
// Get the BackgroundWorker that raised this event.
BackgroundWorker worker = sender as BackgroundWorker;
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("\\\\" + strComputer + "\\root\\CIMV2",
"SELECT Name FROM CIM_DataFile WHERE Extension = 'lck'");
foreach (ManagementObject queryObj in searcher.Get())
{
worker.ReportProgress(0, Convert.ToString(queryObj));
}
}
private void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
listBox1.Items.Add((string)e.UserState);
}
private void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
button1.Enabled = true;
progressBar1.Visible = false;
}
Loading
Nilanka DharmadasaPosted Jan 15, 2010, 12:02 AM
You should use background worker in a situation where you have a time consuming process and that process can be executed in a loop.
It seems in your code, the time consuming process is represented by following line.
searcher.Get() (If i'm worng, please correct me.)
So the complete timeconsuming process is executed by this line. And it is executed only once and returns a collection of 'ManagementObject' objects and what the loop does is looping through this collection.
So what happens is when you run the application, you cannot report the progress time to time. After the timeconsuming process is completed and when it loops through the returned collection, you reports progress. But this looping is not a time consuming work, so you cannot even see this progress reporting with your naked eye.
Evnthough you have used a loop, you dont get the advantage of it.
I hope you got my point.
If my answer helped you to clear your doubt, please tick 'Do you like this answer' checkbox.
JasonPosted Jan 15, 2010, 12:13 AM