views:

129

answers:

1

Sorry, I am very new to all this multithreading stuff...

I'm working on a client/server app and I'm going to use System.Net.Sockets.NetworkStream's Async IO methods.

I'm aware that after calling BeginRead, the system will start calling my callback every time it receives data. The callback could take a significant amount of time to complete but it needs to complete before any other threads run this same callback.

If I simply mark my callback method as synchronized, should that be enough to ensure that each callback thread will run to completion in the order that the system received their data?

If not, what synchronization tool would be best? (e.g. Monitors, WaitHandles, Semaphores?, etc...)

Edit: this is in VB 2008 on Win Server 2008

+1  A: 

Each BeginRead matched a single EndRead. If you want to read more, you have to call BeginRead again in the callback.


BTW, sockets are pretty low level. Are you sure you can't use WCF for this?

John Saunders
I didn't know that. Thanks. I was told before that the system will keep calling the callback until EndRead was called.
mcjabberz
I haven't looked into WCF but this is for a time-clock so I need to be able to fiddle with the bits a bit.
mcjabberz
Time clock, ok, TCP/IP it is. :-)
John Saunders
See the example at http://msdn.microsoft.com/en-us/library/system.net.sockets.networkstream.endread.aspx for where you may have been told about looping.
John Saunders
Also, if you need high performance, see http://msdn.microsoft.com/en-us/library/system.net.sockets.socketasynceventargs.aspx. I haven't used it, but it looks good.
John Saunders