473,569 Members | 2,463 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why callback functions are static?

I need to use Asynchronous Socket functions in a server application and
am learning from sources such as the MSDN2
(http://msdn2.microsoft.com/en-us/library/bbx2eya8.aspx). What I
observed is that all callback handlers in examples are static
functions. I did try non-static callback handlers; they certainly
works, and they usually make my code simpler.

For example, a typical tutorial will start an asynchronous connection
with:

client.BeginCon nect(remoteEP, new AsyncCallback(C onnectCallback) ,
client );

and handle the results with:

static void ConnectCallback (IAsyncResult ar) {
...
}

Because we cannot directly refer to any class member variables and
functions in a static function, we will have to pack them into the "ar"
parameter and pass them to the callback handler. In the above example,
we passed the caller of the BeginConnect() function as the "ar", so we
can get it back with "ar.AsyncState" .

On the other hand, if non-static callback handlers are used, I usually
don't need to pass anything in "ar". In the above example, I will be
able to directly use the "client" in the ConnectCallback () (My
BeginXXX() and handlers are in the same class).

Can somebody please let me know if there will be any performance
penalties if I go with the non-static callbacks? What was the reason
for the penalties if there are any?

I am also curious about how expensive the "new" operation, that coverts
a callback function name to an AsyncCallback delegate, is. In case of
reading and writing, my sever will need to process lots of them. So I
am thinking of creating a member variable to hold the function
references for the lifetime off an object.

Any input will be very appreciated,
Jimmy

Oct 5 '06 #1
4 5873
No performance problem; in the examples, the Connect method itself is
static, so this is just a feature of the design of the example.
Delegates work similarly (identically?) for static and non-static
members, and instance will work fine.

This type of thing is often the result of simple demo code falling
directly from the (enforced) static Main() method...

Marc

Oct 6 '06 #2

Marc Gravell wrote:
No performance problem; in the examples, the Connect method itself is
static, so this is just a feature of the design of the example.
Delegates work similarly (identically?) for static and non-static
members, and instance will work fine.

This type of thing is often the result of simple demo code falling
directly from the (enforced) static Main() method...

Marc
I always assumed they did it that way so they didn't have to worry
about concurrent access to member variables where a callback is raised
from two threads. So it makes the example simpler and architectually
correct, but certainly more confusing. I could well be wrong though :)
Great question imo.

Oct 6 '06 #3
I always assumed they did it that way so they didn't have to worry
about concurrent access to member variables where a callback is raised
from two threads.
Curiously, I always approach it the exact opposite way - i.e. an
instance (non-static) method may, under cetain circumstances, have to
be made thread safe, but static methods should *always* consider thread
safety. Just being static doesn't (alone) enforce anything re thread
safety, and static methods can happily reference static members, and so
need sync. Conversely, with a non-static implementation, there is a
reasonable chance (depending on the design) that each instance
represents a separate request, and so we don't have to worry about sync
- as each callback will be acting on a different instance.

But yes - an intersting question / discussion topic.

Marc

Oct 6 '06 #4
Thanks both of you! I was busy writing a piece of code testing the
performance. It turns out, you guessed it, they are the same and Marc
was right.

Again, really appreciated!
Jimmy

Marc Gravell wrote:
I always assumed they did it that way so they didn't have to worry
about concurrent access to member variables where a callback is raised
from two threads.

Curiously, I always approach it the exact opposite way - i.e. an
instance (non-static) method may, under cetain circumstances, have to
be made thread safe, but static methods should *always* consider thread
safety. Just being static doesn't (alone) enforce anything re thread
safety, and static methods can happily reference static members, and so
need sync. Conversely, with a non-static implementation, there is a
reasonable chance (depending on the design) that each instance
represents a separate request, and so we don't have to worry about sync
- as each callback will be acting on a different instance.

But yes - an intersting question / discussion topic.

Marc
Oct 8 '06 #5

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

Similar topics

1
8845
by: pvdm | last post by:
Hi, I am writing an app which encapsulates a multimedia timer. I implemented a TimerProc as static member function and a static member variable pThis as pseudo this variable to access in the static TimerProc function. timeSetEvent uses TimerProc to set the callback function.
11
2455
by: ajay.sonawane | last post by:
Hello ther I read somewhere that callback function should be global or static member function. 1: I could use static member functions but how could I access members of class which ar not static. 2: I could use global function but want to access members of class. Let me know the possible solutions. Regards, Ajay Sonawane
3
1951
by: Wen | last post by:
hello, now, i wanna port a c++ program into C# project. a DLL written by C++ and keep it no change, and UI wirtten by C#. my C++ UI code is as below: // error handlers --global function callback function // C++ dll would invoke this function if needed void ImageConv_Callback_Handler(const char *szInfo) {
4
5130
by: H.B. | last post by:
Hi, I successfully implement a static callback function for my dll usign delegates. Now, I need to use member function instead of static function. How can I make that (in Managed C++). Hugo
4
5392
by: Dmitri Sologoubenko | last post by:
Hi, guys! I need a C++ class (Linux/POSIX/GNU C++), which can be started and stopped, and calls a virtual callback instance method (e.g. "expired()") when a given time has elapsed. High resolution means that expire time should be measured in microseconds, or at least in milliseconds. I had a look on Gnu C <sys/time.h>'s functions...
2
7353
by: MyCrystalGift | last post by:
Hi, I have an old C++ GUI Application CPPAPP.exe that calls a C DLL library RULE.DLL through a C++ class wrapper LoadRule.CPP. Now I need to call the C DLL RULE.DLL from C# GUI application CSAPP.exe. I need help to convert LoadRule.CPP to a C++ BridgeDLL, and I need help on how to convert the call to the BridgeDLL from C#.
6
7658
by: smmk25 | last post by:
Before I state the problem, I just want to let the readers know, I am knew to C++\CLI and interop so please forgive any newbie questions. I have a huge C library which I want to be able to use in a .NET application and thus am looking into writing a managed C++ wrapper for in vs2005. Furthermore, this library has many callback hooks which...
2
4104
by: Evan Burkitt | last post by:
Hi, all. I have a Windows DLL that exports a number of functions. These functions expect to receive a pointer to a callback function and an opaque void* parameter. The callback functions are typedef'd to take void* parameters, through which the DLL's function passes its void* parameter to the callback function. This allows me to use class...
5
4300
by: Jef Driesen | last post by:
I have a C DLL that I want to use from a C# project. The C header file contains these declarations: typedef void (*callback_t) (const unsigned char *data, unsigned int size, void *userdata); void myfunction (callback_t callback, void *userdata); How do I translate this to C#? I tried with:
0
7703
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...
0
7618
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7926
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. ...
1
7678
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...
0
7982
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...
0
6286
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...
0
3656
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3644
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2116
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

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.