Callbacks and Threads

A

Adam Clauss

Couple questions:
1) I have an application using TCP sockets. When I make a call to
BeginReceive(), is the callback I specify called in the current thread or
from a new thread?

2) Similar to the first, with regards to events. When I actually call an
event (with some number of delegates added to it), is each thread called
sequentially in the calling thread, or is each executed asynchronously in
its own?

I'm running into some wierd behavior in an application I'm writing,
wondering if one or both of the above might be the problem.

Thanks!
 
J

Jon Skeet [C# MVP]

Adam Clauss said:
Couple questions:
1) I have an application using TCP sockets. When I make a call to
BeginReceive(), is the callback I specify called in the current thread or
from a new thread?

The threadpool - by the time the callback is ready to be called, the
current thread will be doing something else!
2) Similar to the first, with regards to events. When I actually call an
event (with some number of delegates added to it), is each thread called
sequentially in the calling thread, or is each executed asynchronously in
its own?

There aren't different threads - just different delegates. Calling the
multicast delegate calls each delegate in turn, on the current thread.
(You can't actually call an event; the C# compiler makes it look like
you can by creating a delegate with the same name as the event if you
do a simple event declaration.)
I'm running into some wierd behavior in an application I'm writing,
wondering if one or both of the above might be the problem.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
A

Adam Clauss

So, let me make sure I got this correct-if I am doing all my processing from
these network callbacks, I DO need to be worried about accessing/modifying
objects shared between them.
However, when an event is triggered, I do not, as they will all occur from
calling thread (assuming calling thread is the only thing accessing things).

My goal is to simplify everything down into a single thread - I am almost
certain that is where my problem is arising from. This should give me a
good place to work from.

Thanks for your help!
 
J

Jon Skeet [C# MVP]

Adam Clauss said:
So, let me make sure I got this correct-if I am doing all my processing from
these network callbacks, I DO need to be worried about accessing/modifying
objects shared between them.

It depends on what you're doing, to be honest, but yes, in general
that's the case.
However, when an event is triggered, I do not, as they will all occur from
calling thread (assuming calling thread is the only thing accessing things).
Yes.

My goal is to simplify everything down into a single thread - I am almost
certain that is where my problem is arising from. This should give me a
good place to work from.

If you want to do everything from a single thread, don't bother using
the asynchronous IO methods to start with.
 
A

Adam Clauss

If you want to do everything from a single thread, don't bother using
the asynchronous IO methods to start with.


Well I don't really want to block either... the time while it waits to
receive and send could be better spent doing other tasks, I'd rather let
that happen asynchronously.
Also, I'm sending/receiving from multiple clients, so if I did not use the
asynchronous methods, I would have to have a different thread for each
client wouldn't I? Not sure that would gain me anything.
 
J

Jon Skeet [C# MVP]

Adam Clauss said:
Well I don't really want to block either... the time while it waits to
receive and send could be better spent doing other tasks, I'd rather let
that happen asynchronously.
Also, I'm sending/receiving from multiple clients, so if I did not use the
asynchronous methods, I would have to have a different thread for each
client wouldn't I? Not sure that would gain me anything.

In that case I'm confused by what you meant about wanting to do
everything in a single thread... you could, of course, start a new
thread (or use a custom thread pool) and do everything synchronously
for one client within that thread.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top