473,401 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

Asynchronous callbacks in C#

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 ;

/* ctors and dtor*/
public Fred(){;}
~Fred(){;}

void foo() {
params = getConfig();

/* Spawn worker thread that will
notify me when completed */
asynchWorker(methodToInvoke, params, myCallback);
}

void methodToInvoke(MyParams p, MyCallBack cb){
//Do something ..
}

//How do you implement MyCallback, in C++ we have functors
// and in C we have function pointers. How is it done
// in C# ?
}

Case 2 (same thread)
This allows me to register a callback so that
Barney can act like a facade pattern
-------------------------------------------
class Barney {

private MyCallback myCall = null ;

/* ctors and dtor*/
public Barney(){;}
~Barney(){;}

void registerCallback(MyCallBack cb) {
if (cb != null) myCall = cb ;
return;
}

//How do you implement MyCallback, in C++ we have functors
// and in C we have function pointers. How is it done
// in C# ?
}
Nov 25 '05 #1
2 2357
Ann Huxtable wrote:
//How do you implement MyCallback, in C++ we have functors
// and in C we have function pointers. How is it done
// in C# ?


look up "delegates", thats C#'s function pointers.

hth,
Max
Nov 27 '05 #2
Ann.... A Delegate in C# is a type safe pointer to some function. You
can then
declare an Event which adds += and -= syntax for the delegate. You can
think of the relationship of an Event to a Delegate as analogous to the
relationship of a Property to a Field.

You can also roll your own callback by declaring an interface INotify
with a
method Notify. In your client implement the INotify interface by
providing a
Notify method and pass a reference (this) of type INotify to the server.
When
the server broadcast it invokes clientRef.Notify.

To show how a Delegate is similar to a C callback here is some C++/cli
code:

// C++ CODE
typedef void (*CALLBACK)();
public class Native {
private:
vector<CALLBACK> vPointers;
public:
void Register(CALLBACK cb){
vPointers.push_back(cb);
}
void Broadcast() {
for (unsigned int i=0; i<vPointers.size(); i++) {
(vPointers[i])();
}
}
...
}

// C++/cli CODE
public delegate void CallbackDelegate();
public ref class Interop {
public:
void Notify() {
Console::WriteLine("Notify");
}

// MAIN UNIT TEST
Interop^ interop= gcnew Interop();
CallbackDelegate^ cd= nullptr;
GCHandle gch;
try {
cd = gcnew CallbackDelegate(interop,&Interop::Notify);
gch = GCHandle::Alloc(cd);
IntPtr ip = Marshal::GetFunctionPointerForDelegate(cd);
CALLBACK cb2 = static_cast<CALLBACK>(ip.ToPointer());
n.Register(cb2);
n.Broadcast();
...
}
finally {
if (gch.IsAllocated) {
gch.Free();
}
}
// n.Broadcast now invalid call

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Nov 27 '05 #3

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

Similar topics

9
by: Sam Loveridge | last post by:
Hi all. I'm relatively new to delegates and asynchronous threading and am running into an issue. I need to asynchronously call a method (which I'm doing with a delegate and BeginInvoke) and from...
4
by: theBoringCoder | last post by:
Hello All, I just started learning about Delegates, and was wondering if someone could point me at some useful, straightforward examples of how to use delegates to make method calls...
4
by: Matthew Groch | last post by:
Hi all, I've got a server that handles a relatively high number of concurrent transactions (on the magnitude of 1000's per second). Client applications establish socket connections with the...
0
by: Kruz | last post by:
Somewhere around 500 connections when calls (BeginSend calls) are arround 108,752 and total bytes sent are around 9,496,399 bytes - send callbacks (EndSend) stop coming in C# asynchronous sockets....
4
by: jim | last post by:
Hi All, I try to make an asynchronous call to a web service method as below under MS visual .NET studio 2003: WebService webSrv = new WebService(); AsyncCallback cb = new...
4
by: taskswap | last post by:
I have a legacy application written in C that I'm trying to convert to C#. It processes a very large amount of data from many clients (actually, upstream servers - this is a mux) simultaneously. ...
1
by: dba123 | last post by:
I need to perform Asynchronous Inserts using DAAB. So far I have a method which does an insert but how can I do this Asyncronously so that it does not affect the load on our public production...
2
by: mark.norgate | last post by:
Hello The thing that interested me more about ASP.NET 2.0 before it came out was asynchronous web pages. Now it's here and I have time to investigate, I'd like to know about how to write pages...
1
by: Alper AKCAYOZ | last post by:
Hello, I have developped asynchronous socket communication with blocking Socket commands (accept, connect, send, receive) by using threads on Windows .NET Forms. It is working properly. Now I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...
0
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,...

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.