473,387 Members | 1,673 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,387 software developers and data experts.

4 way of Asynchronous Programming, what is the differenen between the first two?

Hi,

I read Microsoft SDK,
ms-help://MS.NETFrameworkSDKv1.1/cpguidenf/html/cpovrasynchronousprogramming
overview.htm

there are 4 ways to call EndInvoke:

The code in this topic demonstrates four common ways to use BeginInvoke and
EndInvoke to make asynchronous calls. After calling BeginInvoke you can:

a.. Do some work and then call EndInvoke to block until the call
completes.
b.. Obtain a WaitHandle using IAsyncResult.AsyncWaitHandle, use its
WaitOne method to block execution until the WaitHandle is signaled, and then
call EndInvoke.
c.. Poll the IAsyncResult returned by BeginInvoke to determine when the
asynchronous call has completed, and then call EndInvoke.
d.. Pass a delegate for a callback method to BeginInvoke. The method is
executed on a ThreadPool thread when the asynchronous call completes, and
can call EndInvoke.

What is the difference between the first two way since EndInvoke itself will
block anyway?

BTW, in the second case, will EndInvoke block anymore?

Thanks!
Ryan
Dec 23 '06 #1
5 2285
They are close, but slightly different. The first will call WaitOne on the
handle if not already complete. If it is complete, it will not create the
waithandle which can be more efficent. The second one will always create
the waithandle (i.e. a resetevent) whether the thing is complete or not.
The second would also allow you to wait on other events (i.e. WaitAny, etc)
because you have the handle. Unless you need to "gather" other async
events, the 4th one is recommended as your only called when it is complete
and you also avoid creating the waithandle (and IMO, seems the most
natural). On a busy server, for example, that can add up to some real
savings.

--
William Stacey [C# MVP]

"Ryan Liu" <ad********@online.sh.cnwrote in message
news:eg**************@TK2MSFTNGP06.phx.gbl...
| Hi,
|
| I read Microsoft SDK,
|
ms-help://MS.NETFrameworkSDKv1.1/cpguidenf/html/cpovrasynchronousprogramming
| overview.htm
|
| there are 4 ways to call EndInvoke:
|
| The code in this topic demonstrates four common ways to use BeginInvoke
and
| EndInvoke to make asynchronous calls. After calling BeginInvoke you can:
|
| a.. Do some work and then call EndInvoke to block until the call
| completes.
| b.. Obtain a WaitHandle using IAsyncResult.AsyncWaitHandle, use its
| WaitOne method to block execution until the WaitHandle is signaled, and
then
| call EndInvoke.
| c.. Poll the IAsyncResult returned by BeginInvoke to determine when the
| asynchronous call has completed, and then call EndInvoke.
| d.. Pass a delegate for a callback method to BeginInvoke. The method is
| executed on a ThreadPool thread when the asynchronous call completes, and
| can call EndInvoke.
|
| What is the difference between the first two way since EndInvoke itself
will
| block anyway?
|
| BTW, in the second case, will EndInvoke block anymore?
|
| Thanks!
| Ryan
|
|
Dec 23 '06 #2


"William Stacey [C# MVP]" <wi************@gmail.comдÈëÓʼþ
news:%2****************@TK2MSFTNGP06.phx.gbl...
They are close, but slightly different. The first will call WaitOne on
the
handle if not already complete. If it is complete, it will not create the
waithandle which can be more efficent. The second one will always create
the waithandle (i.e. a resetevent) whether the thing is complete or not.
The second would also allow you to wait on other events (i.e. WaitAny,
etc)
because you have the handle. Unless you need to "gather" other async
events, the 4th one is recommended as your only called when it is complete
and you also avoid creating the waithandle (and IMO, seems the most
natural). On a busy server, for example, that can add up to some real
savings.

--
William Stacey [C# MVP]

"Ryan Liu" <ad********@online.sh.cnwrote in message
news:eg**************@TK2MSFTNGP06.phx.gbl...
| Hi,
|
| I read Microsoft SDK,
|
ms-help://MS.NETFrameworkSDKv1.1/cpguidenf/html/cpovrasynchronousprogramming
| overview.htm
|
| there are 4 ways to call EndInvoke:
|
| The code in this topic demonstrates four common ways to use BeginInvoke
and
| EndInvoke to make asynchronous calls. After calling BeginInvoke you can:
|
| a.. Do some work and then call EndInvoke to block until the call
| completes.
| b.. Obtain a WaitHandle using IAsyncResult.AsyncWaitHandle, use its
| WaitOne method to block execution until the WaitHandle is signaled, and
then
| call EndInvoke.
| c.. Poll the IAsyncResult returned by BeginInvoke to determine when the
| asynchronous call has completed, and then call EndInvoke.
| d.. Pass a delegate for a callback method to BeginInvoke. The method is
| executed on a ThreadPool thread when the asynchronous call completes,
and
| can call EndInvoke.
|
| What is the difference between the first two way since EndInvoke itself
will
| block anyway?
|
| BTW, in the second case, will EndInvoke block anymore?
|
| Thanks!
| Ryan
|
|

When we talking about server saving, I have some concerns.

Whew I use async I/O methods, is try to reduce thread I am using in a C/S
application ( I use one thread per client).

But BeginXxx method will use a thread, then my reducing thread intention
will go vain?

Thanks a lot!
>

Dec 23 '06 #3
"Ryan Liu" <ad********@online.sh.cnwrote:
Whew I use async I/O methods, is try to reduce thread I am using in a C/S
application ( I use one thread per client).

But BeginXxx method will use a thread, then my reducing thread intention
will go vain?
The Begin/End methods don't use threads in the same sense that your "1
thread per client" uses threads. They instead leverage an IO Completion Port
and give you an efficient thread scheduling mechanism.

For some analysis of building a (very) big socket server, see a blog entry I
did a while back:
http://www.coversant.net/Default.asp...=88&EntryID=10
--
Chris Mullins, MCSD.NET, MCPD:Enterprise
http://www.coversant.net/blogs/cmullins
Dec 26 '06 #4

| In the second case, EndInvoke is not asynchronous so it technically
"blocks"
| but it doesn't wait on anything and simply returns your result.

IIRC, the EndInvoke will not block or even call waitone, because the result
is already complete (so it skips that step and returns result).
--wjs
Dec 27 '06 #5
Hi Chirs,

Thanks a lot for your reply and article.

First, a basic general question, when will the callback method be called?
Right after BeginReceive() or when the data is available? It shoulb be
called in the same thread which receives data and is called after data is
available, right?

Is that possible to show a few lines sample to demonstrate your idea?

For stage 3:

Is that(if not, what about)per Processing Queue per client/socket?

And where we put the IAsncResult in the queue? From stage 3 diagram, seems
this is done inside BeginReceiveCallBack method. What about we put it into
queue by code immediate follow socket.BeginReceive().

Between the EndRecevie BeginReceive, the socket is not receiving data (if
socket buffer is full) and could block client. Is this the same case for
stage 2 and 3? So the stage 3 does not solve one of the problem stage 2 has?
So is that better to take processing time out of time between EndReceive and
BeginReceive? That is call BeginReceive right after put IAsncResult in the
queue, instead call it at the end of process.
And clients send different kind of message and vary in size, when I call
BeginReceive, how can I specify the size? How can I make sure I won't get
into the middle of message, e.g. get 1.5 message in byte[]?

BeginReceive will use thread in thread pool. And for threads that processing
queue, are explicitly created by our code, right?

But if as I said, call BeginReceive right after put IAsncResult in the
queue, then we never explicitly create any threads and we seems call
BeginReceive more frequently and use more threads in thread pool. Is this
right statement?

Thanks a lot!
Ryan


"Chris Mullins" <cm******@yahoo.comдÈëÓʼþ
news:eU**************@TK2MSFTNGP03.phx.gbl...
"Ryan Liu" <ad********@online.sh.cnwrote:
Whew I use async I/O methods, is try to reduce thread I am using in a
C/S
application ( I use one thread per client).

But BeginXxx method will use a thread, then my reducing thread intention
will go vain?

The Begin/End methods don't use threads in the same sense that your "1
thread per client" uses threads. They instead leverage an IO Completion
Port
and give you an efficient thread scheduling mechanism.

For some analysis of building a (very) big socket server, see a blog entry
I
did a while back:
http://www.coversant.net/Default.asp...=88&EntryID=10
--
Chris Mullins, MCSD.NET, MCPD:Enterprise
http://www.coversant.net/blogs/cmullins


Dec 27 '06 #6

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

Similar topics

6
by: ... | last post by:
Does anyone know a good tutorial on asynchronous programming in .net AsyncCallback And IASyncResult are driving me crazy. And the msdn documentation is not really helpful on this topic I...
4
by: Chris | last post by:
Hello, With asynchronous programming : Why does the callback-function (running in a worker thread) may not update the state of a control on the main-form ? The docs say that you must use a...
5
by: Daniel J Rodriguez | last post by:
Greetings everyone! This is a fairly broard question.. But should be enough to suffice for an answer. Currently we are making an entreprise database server. We will be using C# - my question is...
9
by: Michael Lindsey | last post by:
I need to write a server app to send images to client GUIs that are outside of the server's domain. The client will have the file system path to the image but can not access the file system. I am...
4
by: bernardpace | last post by:
Hi, I am trying to get more familiar with asynchronous programming. I was reading through the document found on the page: ...
3
by: Anders Both | last post by:
Does someone know´s about using threads and/or asynchronous programming inside asp.net (inside the asp.net process). I am here not speaking about the thing that the IIS makes new threads for each...
1
by: Julian Hershel | last post by:
Reading about asynchronous programming (ms-help://MS.NETFrameworkSDK/cpguidenf/html/cpconasynchronousdesignpatterno verview.htm) I could not clarify some doubts. Hope you can help me. 1) Are...
3
by: =?Utf-8?B?bWs=?= | last post by:
Hi everyone, I need to refactor some of our processes using the asynchronous programming model. I have defined a couple of arrays of delegates to pipline the asynchronous invocations through...
3
by: senfo | last post by:
I recently read an MSDN article by Jeff Prosise titled, Scalable Apps with Asynchronous Programming in ASP.NET (http://msdn.microsoft.com/msdnmag/issues/07/03/WickedCode/). In the article, Jeff...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.