1

According to this post:

How to write a scalable Tcp/Ip based server

jerrylvl states:

----------*

Processing

When you get the callback from the Begin call you made, it is very important to realise that the code in the callback will execute on the low-level IOCP thread. It is absolutely essential that you avoid lengthy operations in this callback. Using these threads for complex processing will kill your scalability just as effectively as using 'thread-per-connection'.

The suggested solution is to use the callback only to queue up a work item to process the incoming data, that will be executed on some other thread. Avoid any potentially blocking operations inside the callback so that the IOCP thread can return to its pool as quickly as possible. In .NET 4.0 I'd suggest the easiest solution is to spawn a Task, giving it a reference to the client socket and a copy of the first byte that was already read by the BeginReceive call. This task is then responsible for reading all data from the socket that represent the request you are processing, executing it, and then making a new BeginReceive call to queue the socket for IOCP once more. Pre .NET 4.0, you can use the ThreadPool, or create your own threaded work-queue implementation.

----------*

My question is how exactly would I be doing this in .Net 4.0? Could someone please provide me with a code example which would work well in a scalable environment?

Thanks!

Community
  • 1
  • 1
cjones26
  • 3,459
  • 1
  • 34
  • 51
  • 1
    he is referring to Task Parallel Library i .NET 4 (http://msdn.microsoft.com/en-us/library/dd460717(VS.100).aspx). You can queue up a work item by starting a new task - `Task.Factory.StartNew(()=> { // do work here });` – VinayC Jan 31 '11 at 08:08
  • Understood. When using this sort of method, you are not in fact launching a separate thread each time the callback is called, correct? – cjones26 Jan 31 '11 at 19:08

1 Answers1

0

My question was answered in more depth here: C# - When to use standard threads, ThreadPool, and TPL in a high-activity server

This goes into specifics on using each the TPL, ThreadPool, and standard threads to perform work items, and when to use each method.

Community
  • 1
  • 1
cjones26
  • 3,459
  • 1
  • 34
  • 51