M
mp
c# 2008express
Trying to understand the backgroundworker object.
given a WPF form with button, progressbar and ListBox
Button_Click starts a file search,reading files, parsing text, finding
target strings
Using backgroundWorker so progress bar will update.
Got that more or less working by using the ReportProgress method and
worker_ProgressChanged event
I'd also like to fill in listbox or label with current filename being
searched etc
I'm not seeing how to interact with other form controls during the "time
consuming operation"
Since I can't access the Controls directly as they are on different thread
alternately how could I pass back a filled in SortedDictionary from the
worker thread?
private void button2_Click(object sender, RoutedEventArgs e)
{
listBox1.Items.Clear();
progressBar1.Minimum = 0;
progressBar1.Maximum = 5000;//i'd like to get this value from the
Searchfiles routine (number of files found)
//but how to do???
SearchFilesInBackground( sender, e);
}
void SearchFilesInBackground(object sender, RoutedEventArgs e)
{
BackgroundWorker worker;
worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;
worker.WorkerSupportsCancellation = true;
worker.DoWork += worker_Dowork;
worker.ProgressChanged += worker_ProgressChanged;
worker.RunWorkerCompleted += worker_RunWorkerCompleted;
worker.RunWorkerAsync();
}
void worker_Dowork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker1 = sender as BackgroundWorker;
e.Result = SearchFiles(worker1, e );
}
private object SearchFiles(object sender, DoWorkEventArgs e )
{
BackgroundWorker worker1 = sender as BackgroundWorker;
foreach(string FolderName in folderNames )
{
foreach (string FileName in
System.IO.Directory.GetFiles(FolderName,"*.lsp"))
{
worker1.ReportProgress(i++);//this worked to move progress bar, but how to
send back filenames or collections?
//read file, parse,fill collections//etc
listBox1.Items.Add Filename;// this won't work because listbox1 not
available on this thread!?!
}}
thanks
mark
Trying to understand the backgroundworker object.
given a WPF form with button, progressbar and ListBox
Button_Click starts a file search,reading files, parsing text, finding
target strings
Using backgroundWorker so progress bar will update.
Got that more or less working by using the ReportProgress method and
worker_ProgressChanged event
I'd also like to fill in listbox or label with current filename being
searched etc
I'm not seeing how to interact with other form controls during the "time
consuming operation"
Since I can't access the Controls directly as they are on different thread
alternately how could I pass back a filled in SortedDictionary from the
worker thread?
private void button2_Click(object sender, RoutedEventArgs e)
{
listBox1.Items.Clear();
progressBar1.Minimum = 0;
progressBar1.Maximum = 5000;//i'd like to get this value from the
Searchfiles routine (number of files found)
//but how to do???
SearchFilesInBackground( sender, e);
}
void SearchFilesInBackground(object sender, RoutedEventArgs e)
{
BackgroundWorker worker;
worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;
worker.WorkerSupportsCancellation = true;
worker.DoWork += worker_Dowork;
worker.ProgressChanged += worker_ProgressChanged;
worker.RunWorkerCompleted += worker_RunWorkerCompleted;
worker.RunWorkerAsync();
}
void worker_Dowork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker1 = sender as BackgroundWorker;
e.Result = SearchFiles(worker1, e );
}
private object SearchFiles(object sender, DoWorkEventArgs e )
{
BackgroundWorker worker1 = sender as BackgroundWorker;
foreach(string FolderName in folderNames )
{
foreach (string FileName in
System.IO.Directory.GetFiles(FolderName,"*.lsp"))
{
worker1.ReportProgress(i++);//this worked to move progress bar, but how to
send back filenames or collections?
//read file, parse,fill collections//etc
listBox1.Items.Add Filename;// this won't work because listbox1 not
available on this thread!?!
}}
thanks
mark