473,698 Members | 2,440 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3239
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 AppendTextDeleg ate(string text);

Then from search thread:

Class Search
{
...
private AppendTextDeleg ate asyncUpdate = new
AppendTextDeleg ate(frmMain.Upd ateText);
...

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

} // end Search class

Hope this helps
Cordell Lawrence
Teleios Systems Ltd.

"lltaylor" <ll**********@y ahoo.com> wrote in message
news:ac******** *************** ***@posting.goo gle.com...
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.FindFile s( ); // 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.FindFile s );
asyncMethod.Beg inInvoke( null, null );
}

This way the search runs on a worker thread (in this case a thread pool
thread) and will call you TextBox.BeginIn voke 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**********@y ahoo.com> wrote in message
news:ac******** *************** ***@posting.goo gle.com...
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
7136
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 threads are complete, the call the Callback method. Each "thread" calls the same callback method. What thread does this callback method exist on? My testing indicates the
10
2951
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 exactly what I want. Here are my goals 1. I would like the IAsyncResult that is returned by the Begin function to be able to be waited on or polled to check for completion. 2. The Begin function would take a callback, and the async process would
8
5859
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 unloads. When the callback method is raised, only the method in the web page is run and the page never refreshes, it seems it all happens on the server side. I am trying to refresh the constrols on the page inside the callback method, but when id...
5
3254
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 btnUpload_Click(object sender, System.EventArgs e) { try { SOPWebService.Client uploader = new
1
4320
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 method calling, but what if you are already in a worker thread in a Windows Forms application when doing the web service call. In this case there is no need to use Async Begin..End features that .NET kindly presents us with.
6
3815
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 cause these to block? 2) You can connect together a binaryreader to a networkstream:
7
2854
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 send the messagetype via async NetworkStream ns = client.GetStream(); //assume client globally accessible
11
8602
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 mechanisms. I use a non-blocking approach for this, using the call to 'poll' to support the async mechanism - rather than the 'begin' and 'end' functions. I already found that connecting doesn't set the "isconnected" variable correctly...
10
4499
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 consideration here is that it is supplied to the AsyncOperationManager.CreateOperation(userSuppliedState) method... with userSuppliedState being, more or less, a taskId. In this case, the userSuppliedState {really taskId} is of the object type,...
4
5638
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 it won't recognizine the async method. I'm thinking I have to use the async method of the WS because the WS takes a while to do it's thing and return. If it doesn't take a while, everything works fine. If it does take a while, my portlet becomes...
0
8608
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8897
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8867
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4370
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2332
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2006
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.