473,800 Members | 2,457 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Callbacks and Threads

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!

--
Adam Clauss
ca*****@tamu.ed u

Nov 17 '05 #1
5 2249
Adam Clauss <ca*****@nospam .tamu.edu> wrote:
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.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
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!

--
Adam Clauss
ca*****@tamu.ed u

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Adam Clauss <ca*****@nospam .tamu.edu> wrote:
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.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #3
Adam Clauss <ca*****@nospam .tamu.edu> wrote:
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.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
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.

--
Adam Clauss
ca*****@tamu.ed u
Nov 17 '05 #5
Adam Clauss <ca*****@nospam .tamu.edu> wrote:
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.


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.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1362
by: Dave Benjamin | last post by:
Is there a straightforward way to create a generator from a function that takes a callback? For instance, I have a function called "process": def process(text, on_token): ... For each token that "process" finds in "text", it creates a token object, "token", and calls "on_token(token)". Now, suppose I wanted to create a generator based on this function. I tried
4
2740
by: Randall Hopper | last post by:
What is the correct way to propagate exceptions from Python callbacks? When I do this: Python -> C++ -> Python Callback (example attached) an exception raised in the callback doesn't make it back across C++ to Python. It appears that PyGILState_Release() at the bottom of the callback
10
7476
by: Alfonso Morra | last post by:
This may be considered as OT since the C++ Standard says not one word about threads. Nevertheless, C++ is routinely and widely used to write solid multithreaded code. I wondered if anyone has any pointers/references to invoking callbacks accross different threads? (google is not showing much promise, and I'm running out of both time and patience...)
2
2386
by: Ann Huxtable | last post by:
Hi, I want to do two types of async callbacks in C#. One involves spawning a worker thread, and the other does not invlve threads: Case 1 (Span new thread) ---------------------------- class Fred { private MyParams params = null ;
9
3346
by: zholthran | last post by:
Hi folks, after reading several threads on this issue (-> subject) I fear that I got a problem that cannot easily be solved by the offered workarounds in an acceptable way, at least not with my limited c & c++ experience. Maybe some of you can help. the problem: I need several instances of a class whose (non-static!) methods should serve as callbacks for a dll (which can' be manipulated/adapted in any
1
2204
by: geoffschmidt | last post by:
I'm trying to write an extension in C that delivers callbacks to Python. The C code starts several threads, and I'd like one of the new threads that is started to be able to deliver callbacks to Python. I thought I could do this by wrapping the callback function in PyGILState_Ensure / PyGILState_Release. When I do this, the Python code in the callback in between those two calls certainly works, but when PyGILState_Release is called, the...
4
2233
by: emma_middlebrook | last post by:
Hi Advice needed about what's the best in the following situation. In essence, I have a GUI that needs to detail time taken to do jobs that execute in their own thread. Currently, the GUI thread instantiates a class that wraps a job, handing it a callback (delegate) to call once the job has finished. On callback, always on a different thread than the GUI thread,
1
1898
by: brekehan | last post by:
I am going to try and keep this non-OS specific, especially when I am trying to implement this on multiple OSes The need has arisen for a callback mechanism. Currently, things are going to go something like this: 1) Application requests some work to be done from API and supplies a memory buffer that cannot be modified until called back 2) API does work, fills buffer, and calls back Application when completed <--I am working on this...
15
8415
by: dennis.richardson | last post by:
Greetings all. Here's a problem that's been driving me nuts for the last 48 hours. I'm hoping that someone has come across this before. I have a C# Application that reads a UDP broadcast (asynchronously). Then it repackages these UDP packets and sends them to a subscriber via TCP. Now, I can read the UDP stream all day long without the application
0
9691
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10505
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10253
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10035
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9090
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6813
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.