BackgroundWorker newbie in WPF

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
 
P

Peter Duniho

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?

IMHO, the ideal solution is to create a simple container class to hold
whatever data you want to pass back during progress updates, and then use
the ReportProgress() overload that allows you to pass an object along with
the integer percentage value. Just cast that object reference (retrieved
from the ProgressChangedEventArgs object) back to the appropriate type in
the ProgressChanged event handler, and get whatever data you put in there.

An alternative solution would be to use the Dispatcher.Invoke() method to
execute the additional code you desire (you can get the Dispatcher
reference from the Dispatcher property of a Dispatcher-owned object, such
as your ListBox instance). Note, however, that doing this renders the use
of the BackgroundWorker object somewhat pointless, since one of the main
points of using BackgroundWorker is to avoid explicit calls to an Invoke()
method.

Pete
 
M

mp

Peter Duniho said:
IMHO, the ideal solution is to create a simple container class to hold
whatever data you want to pass back during progress updates, and then use
the ReportProgress() overload that allows you to pass an object along with
the integer percentage value. Just cast that object reference (retrieved
from the ProgressChangedEventArgs object) back to the appropriate type in
the ProgressChanged event handler, and get whatever data you put in there.

An alternative solution would be to use the Dispatcher.Invoke() method to
execute the additional code you desire (you can get the Dispatcher
reference from the Dispatcher property of a Dispatcher-owned object, such
as your ListBox instance). Note, however, that doing this renders the use
of the BackgroundWorker object somewhat pointless, since one of the main
points of using BackgroundWorker is to avoid explicit calls to an Invoke()
method.

Pete
Thanks
I'd not noticed the overload with object alternative, sounds like that would
work
mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top