Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple Asyncronous Tutorial?

Status
Not open for further replies.

ralphtrent

Programmer
Jun 2, 2003
958
0
0
US
Hi,
I am looking for a simple Asyncronous tutorial. All the box I have to not show a clear picture.

Thanks
 
I don't have a pointer to a tutorial for you, but some of the basic concepts (at least for socket communications) are:

1) You call BeginXxxx(), and control returns to you almost immediately. The CLR, under the hood, is making operating system calls on your behalf (IOCompletionPorts). The runtime will call you back later when it's done doing what you asked.

2) You define a state object that hold your send and receive byte-array buffers. When you make a BeginXxxx() call, the memory of this buffer is pinned by the CLR, and the network hardware is then able to write directly to that memory, saving lots of memcpy() operations. As a result it's very fast.

3) When the operation completes, the CLR will call you back on the delegate you registered. In the case of a receive, it will tell you how many bytes it received (up to the size of your byte array). You then append that onto a stringbuilder to build up a application-level message. If you haven't received everything, you issue another BeginReceive() call. In the case of a send, you move some more message data into the send buffer, and issue another BeginSend to tell the network layer to send some more. Note that not everything you asked to be sent may really have been sent.

4) All these operations (BeginSend, BeginReceive) work in a system-provided threadpool, so the thing you have to remember is that when you're in a callback method, you're running on someone else's thread.

Hope this helps.
Chip H.


____________________________________________________________________
Donate to Katrina relief:
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top