OnReceive Question

  • Thread starter Thread starter minnmaxx
  • Start date Start date
M

minnmaxx

I have the following piece of code:

// this called when some data comes in
public void OnReceive( IAsyncResult ar )
{
// receive data
string message = ...;

// continue to listen
BeginReceive( ... );

// process string based on some condition
if ( condition )
{
ComplicatedProcessor( message );
}
else
{
QuickProcessor( message );
}
}

my question is: If the code ends up calling ComplicatedProcessor, and
before ComplicatedProcessor returns, there's an incoming message, will
OnReceive be able to be called again?.. meaning while the 1st
OnReceive's still running, can a 2nd one start?
From my experiment, the answer is no. Everyone know how can I get
around the problem?
 
minnmaxx said:
I have the following piece of code:

// this called when some data comes in
public void OnReceive( IAsyncResult ar )
{
// receive data
string message = ...;

// continue to listen
BeginReceive( ... );

// process string based on some condition
if ( condition )
{
ComplicatedProcessor( message );
}
else
{
QuickProcessor( message );
}
}

my question is: If the code ends up calling ComplicatedProcessor, and
before ComplicatedProcessor returns, there's an incoming message, will
OnReceive be able to be called again?.. meaning while the 1st
OnReceive's still running, can a 2nd one start?

around the problem?

OnReceive runs on a threadpool thread, so when a message arrives before the
ComplicatedProcessor method returns another thread will be pulled from the
pool to run OnReceive.
What makes you think the answer is no.

Willy.
 
Claire said:
Let another thread handle the processing?

Why? Each OnReceive runs on a Threadpool thread, there is no need to spawn
yet another one.

Willy.
 
Back
Top