473,378 Members | 1,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Async Method Help

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
Nov 17 '05 #1
5 3210
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.

"lltaylor" <ll**********@yahoo.com> wrote in message
news:ac**************************@posting.google.c om...
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

Nov 17 '05 #2
http://msdn.microsoft.com/msdnmag/is...ultithreading/

Excellent article on this stuff.

Cordell Lawrence
Teleios Systems Ltd.
Nov 17 '05 #3
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?
Nov 17 '05 #4
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.

"lltaylor" <ll**********@yahoo.com> wrote in message
news:ac**************************@posting.google.c om...
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?

Nov 17 '05 #5

That did it.

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

Thanks for your help.

Lloyd
*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: scott ocamb | last post by:
hello I have implemented a solution using async methods. There is one async method that can be invoked multiple times, ie there are multiple async "threads" running at a time. When these...
10
by: Shawn Meyer | last post by:
Hello - I am trying to write a class that has an async BeginX and EndX, plus the regular X syncronous method. Delegates seemed like the way to go, however, I still am having problems getting...
8
by: TS | last post by:
Im in a web page and call an asynchronous method in business class. the call back method is in the web page. When page processes, it runs thru code begins invoking the method then the page...
5
by: Paul Hasell | last post by:
Hi, I'm trying to invoke a web method asynchronously but just can't seem to get it to tell me when it has finished! Below is the code I am (currently) using: private void...
1
by: Simon Hart | last post by:
Hi, I thought I'd just open a thread in an attempt to get peoples feelers with regards to multithreading vs Async Web Service processing. Of course Web Services makes it easy to do Async...
6
by: Shak | last post by:
Hi all, Three questions really: 1) The async call to the networkstream's endread() (or even endxxx() in general) blocks. Async calls are made on the threadpool - aren't we advised not to...
7
by: Shak | last post by:
Hi all, I'm trying to write a thread-safe async method to send a message of the form (type)(contents). My model is as follows: private void SendMessage(int type, string message) { //lets...
11
by: atlaste | last post by:
Hi, In an attempt to create a full-blown webcrawler I've found myself writing a wrapper around the Socket class in an attempt to make it completely async, supporting timeouts and some scheduling...
10
by: Frankie | last post by:
It appears that System.Random would provide an acceptable means through which to generate a unique value used to identify multiple/concurrent asynchronous tasks. The usage of the value under...
4
by: dlc9s | last post by:
Hi All, I have a JSR 168 portlet that I need to call a J2EE 1.4 JAX-RPC Web Service. I'm using Oracle 10g JDeveloper. (I don't have a choice about this). It works when I call the sync method, but...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.