Jason

Jason

  • NA
  • 3
  • 0

DoWork() method not executing

Jan 13 2010 11:01 PM

  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; 
       
}
 

Answers (2)