Async Method Help

  • Thread starter Thread starter lltaylor
  • Start date Start date
L

lltaylor

Hello,

Help, I am totally stuck, I am trying to write a sample program so I
can get to grips with Asynchronous Threads etc... However I can't come
up with a good workflow for my program.

Any help in what I should be doing/how I should structure my
application would be much appreciated.

I have written a sample application which bascially searches a set
folder structure and outputs all .txt files found within that
directory tree to the textbox on the UI, the current form of the
program is that the UI thread does all this - so there is no update to
the UI once the user presses scan, until it finishes - which is
obviously the wrong way to go about things.

I have two classes
1) The Search Class
This scans the directories and any .txt files it finds, it calls a
method
in the UI class, which inturn updates the textbox on the UI
2) The UI
i) With a button click event to start the search
ii)An update method which writes to the textbox

I want the updates to textbox to be carried out by the worker thread
so I can see the progress in real time.

Hows do I do this, and where?
I know I should be calling a BeginInvoke etc etc, but I can't for the
life of me figure out where it should go.

Any help would be a blessing.

Lloyd
 
Hey Lloyd,
Sounds like you have most of the things you need to do what you want.
First problem that I noticed is that you said you want the updates to the
text box to take place on the worker thread. This should actually be the
other way around, all UI updates should be done on the Main application
thread *(or the thread on which the controls were created[will need to
double check this]), and other intensive time consuming tasks be done on
worker threads. So the first thing that you want to do is to let your search
run on a worker thread, that way you free up the main UI thread to continue
updating / responding to your UI events. Second from your worker thread,
*ask* the main thread to update your UI by calling the Text box's
BeginInvoke method. Some advice -

Create a delegate that takes the params needed to update you text box e.g.

delegate void AppendTextDelegate(string text);

Then from search thread:

Class Search
{
...
private AppendTextDelegate asyncUpdate = new
AppendTextDelegate(frmMain.UpdateText);
...

void Search(...)
{
...
// To update the UI on the main thread do
frmMain.TextBox.BeginInvoke(asyncUpdate, new object[] { fileName });
...
}

} // end Search class

Hope this helps
Cordell Lawrence
Teleios Systems Ltd.
 
Thanks for those post's very useful.

In a previous implementation I tried your suggestion of the
BeginInvoke on the textbox, and what I find is that the UI doesn't
freeze however the updates to the text box don't happen until the scan
of the directory has completed.

Is this because when I make a call to the search class, that is being
run on the UI thread and hence can't be updated till it has finished
running the search?
 
Well, I can say definitively because I don't have your code, but what you do
is something like the following:

private void btnSearchClick(object sender, EventArgs e)
{
// Get root path
string rootDir = tbxPath.Text;
FileSearch search = new FileSearch ( rootDir );

search.FindFiles( ); // search runs on the calling thread which is (in
most cases) the main thread
}

Then the answer would be yes, because you are performing the search from the
Main thread with is the thread that updates the UI.
What you should do is to put this searh work on another thread. you can use
delegates or your own thread if you like.

using System.Windows.Forms; // for MethodInvoker

private void btnSearchClick(object sender, EventArgs e)
{
// Get root path
string rootDir = tbxPath.Text;
FileSearch search = new FileSearch ( rootDir );

MethodInvoker asyncMethod = new MethodInvoker( search.FindFiles );
asyncMethod.BeginInvoke( null, null );
}

This way the search runs on a worker thread (in this case a thread pool
thread) and will call you TextBox.BeginInvoke to update the text box, this
time though your main thread will be able to update the UI because it's not
busy.

Hope this helps.
Cordell Lawrence
Teleios Systems Ltd.
 
That did it.

Sorry for such lame questions, but I wanted to make sure I understood
this all correctly.

Thanks for your help.

Lloyd
 
Back
Top