Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2006

Tip: Looking for answers? Try searching our database.

Disadvantages of using calling methods asynchronously using C#

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
GVN - 19 Aug 2006 13:02 GMT
Hi All,

   Can anyone guide me when asynchronous method calls will be
benificial? Are there any

disadvantages of using asynchronous calls?

Thanks,

GVN
Peter Bromberg [C# MVP] - 19 Aug 2006 13:39 GMT
GVN,
Let's make it simple: Say you have an app that needs to get rss feeds from
1000 sites once every hour.  You can serialize it by having one thread go
through all 1000 requests one at a time, or you can do it asynchronously on a
threadpool with say 30 threads, in which case you will have 30 requests going
simultaneously and as soon as a thread has finished it will pick up a new
request. This may not turn out to be exactly 30 times faster than the first
case, but I think you get the idea.

Disadvantages? You have to understand how to do it correctly, and that takes
some study.
Peter

Signature

Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com

> Hi All,
>
[quoted text clipped - 6 lines]
>
> GVN
Vadym Stetsyak - 19 Aug 2006 13:41 GMT
Hello, GVN!

G>     Can anyone guide me when asynchronous method calls will be
G> benificial?

Its up to app design. Async method calls are used when the caller
knows that operation will take a while, so she doesn't wait for method to complete
and instead can do some more usefull job ( render UI etc ).

Good example is network I/O. If you have GUI application it will not be good if UI
freezes every time you're receiving or sending data, right?

G> Are there any disadvantages of using asynchronous calls?
Usually making synchronous call is faster that async one.
This statement is valid only of method execution time is relatively small...

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
steve.falzon@<nospamplease>noonbay.co.uk - 19 Aug 2006 14:58 GMT
>Hi All,
>
[quoted text clipped - 6 lines]
>
>GVN

The BackgroundWorker class makes implementing asynch methods a breeze.
Take a look at the page below it gives a decent explanation of why you
would use asynch calls and how to use the BackgroundWorker class.

http://msdn2.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx

cheers

Steve
John Vottero - 19 Aug 2006 18:05 GMT
> Hi All,
>
>    Can anyone guide me when asynchronous method calls will be
> benificial? Are there any
>
> disadvantages of using asynchronous calls?

There have been some replies that confuse multithreading with asynchronous
method calls.  The two aren't the same.  Threadpools and backgroundworker
threads are great but, they don't solve the same problems that async method
calls can solve.

Another reply gave an example of checking 1000 RSS feeds with a pool of 30
threads.  The asynchronous approach would be to issue 1000 BeginRead() so
you have 1000 outstanding async calls then, as the reads complete, the
AsyncCallback method is called.

If you were writing a CHAT server, you could create a new thread for each
client but, that wouldn't scale very far.  With an async approach, you would
call BeginRead() for each client then, when the read completes, the
AsyncCallback would call BeginWrite() for each client that the incoming
message should be directed to and then call BeginRead() again to replace the
read that just completed.  This would scale to thousands of clients with
just a few threads.
Jeff Louie - 19 Aug 2006 20:10 GMT
John.. I don't completely understand your point since from what I see
async
method calls to COM and BeginInvoke in C# are both implemented
internally
using threadpools.

Regards,
Jeff
>Threadpools and backgroundworker
threads are great but, they don't solve the same problems that async
method
calls can solve.<
John Vottero - 20 Aug 2006 18:50 GMT
> John.. I don't completely understand your point since from what I see
> async
> method calls to COM and BeginInvoke in C# are both implemented
> internally
> using threadpools.

The only point I was trying to make is that there's a difference between
multithreaded programming (which is inherently asynchronous) and
asynchronous method calls.  It's important to understand the difference
between calling ThreadPool.QueueUserWorkItem and BeginRead.  I'm not saying
that one is better than the other, they're just different.
steve.falzon@<nospamplease>noonbay.co.uk - 19 Aug 2006 20:17 GMT
>> Hi All,
>>
[quoted text clipped - 20 lines]
>read that just completed.  This would scale to thousands of clients with
>just a few threads.

Hi John

Granted asynch calls and multithreading aren't the same thing.
However, it is often desirable to execute an asynch calls on it's own
thread, as is the case when trying to avoid an unresponsive  UI in a
windows app for instance, the two methods can be used together. Or so
I thought.

If I'm mistaken I'd appreciate your advice on how best to approach
this. Assuming I'm not looking to scale to thousands of users, just a
single user using the app, how should I ensure the app remains
responsive whilst waiting for a request, to a webservice for instance,
to complete? For me the BackgroundWorker solves this problem and with
less effort than has previously been the case.

There are many ways to approach a problem and I'd be interested to
know how others might solve problems like this.

Cheers

Steve
John Vottero - 20 Aug 2006 18:58 GMT
>>> Hi All,
>>>
[quoted text clipped - 31 lines]
> windows app for instance, the two methods can be used together. Or so
> I thought.

I didn't mean to imply that asynch calls were better than multithreading, I
just wanted to point out the difference.

> If I'm mistaken I'd appreciate your advice on how best to approach
> this. Assuming I'm not looking to scale to thousands of users, just a
> single user using the app, how should I ensure the app remains
> responsive whilst waiting for a request, to a webservice for instance,
> to complete? For me the BackgroundWorker solves this problem and with
> less effort than has previously been the case.

I use BackgroundWorker threads all the time, I think they're great.  But, if
you might have hundreds of outstanding webservice calls, you probably don't
want to have a thread for each one of them.

> There are many ways to approach a problem and I'd be interested to
> know how others might solve problems like this.
>
> Cheers
>
> Steve
Jon Skeet [C# MVP] - 20 Aug 2006 05:52 GMT
> There have been some replies that confuse multithreading with asynchronous
> method calls.  The two aren't the same.  Threadpools and backgroundworker
> threads are great but, they don't solve the same problems that async method
> calls can solve.

To be clear here - they aren't *always* the same. They sometimes are.
Basically when you use asynchronous *IO* you use IO completion ports
rather than a single thread per connection.

However, other types of asynchronous call (SomeDelegate.BeginInvoke,
Control.BeginInvoke etc) *are* just multithreading using the
threadpool.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Willy Denoyette [MVP] - 20 Aug 2006 20:31 GMT
| > There have been some replies that confuse multithreading with asynchronous
| > method calls.  The two aren't the same.  Threadpools and backgroundworker
[quoted text clipped - 8 lines]
| Control.BeginInvoke etc) *are* just multithreading using the
| threadpool.

What makes you think that Control.BeginInvoke uses the threadpool?, it
simply queues a delegate and posts a message to the windows message queue
associated with the Control's HWND, the threadpool is not used here.
Willy.
Jon Skeet [C# MVP] - 21 Aug 2006 01:03 GMT
> What makes you think that Control.BeginInvoke uses the threadpool?, it
> simply queues a delegate and posts a message to the windows message queue
> associated with the Control's HWND, the threadpool is not used here.

Sorry, yes - don't know what I was thinking of there. I'm pretty sure
about Delegate.BeginInvoke though :)

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.