473,287 Members | 1,560 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,287 software developers and data experts.

Passing events as arguments to functions

It seems like there should be a way to pass events around like
delegates but on the other hand they’re sort of special delegates
because the only one allowed to invoke them is the owner of the event.
What I want to do is create a generic event forwarder that forwards
events from a server to a client. It will only subscribe to the
server events once and only if there’s at least one client
subscriber. Once the last client subscriber removes its event handler
then the forwarder will remove its event handler from the server.

To use this class it would look something like this:
m_Forwarder = new GenericEventForwarder<MyArgs>((object)this);
// pass this pointer so forwarder can use as sender of events so
clients think that we're the sender

public event EventHandler<MyArgsSomethingChanged
{
Add
{
m_Forwarder.Add(value, new
EventHandler<MyArgs>(m_Server.SomethingChanged));
}
Remove
{
m_Forwarder.Remove(value, new
EventHandler<MyArgs>(m_Server.SomethingChanged));
}
}

The forwarder class would maintain it’s own delegate chain via
Delegate.Combine() and Delegate.Remove() keeping track of the count of
delegates.

Sep 17 '08 #1
2 1566
Israel <is**********@hotmail.comwrote:
It seems like there should be a way to pass events around like
delegates but on the other hand they=3Fre sort of special delegates
because the only one allowed to invoke them is the owner of the event.
They're not delegates at all. They're a pair of methods - subscribe and
unsubscribe. Talking about passing them around is like talking about
passing a property around (rather than the *value* of a property). You
can of course pass a PropertyInfo (or an EventInfo) if necessary.
What I want to do is create a generic event forwarder that forwards
events from a server to a client. It will only subscribe to the
server events once and only if there=3Fs at least one client
subscriber. Once the last client subscriber removes its event handler
then the forwarder will remove its event handler from the server.
That sounds feasible. In that case, the EventInfo would actually give
you everything you need - admittedly you'd be calling the
subscribe/unsubscribe bits by reflection (unless you created a delegate
to do that - very confusing!) but it would do the trick.
<snip>
The forwarder class would maintain it=3Fs own delegate chain via
Delegate.Combine() and Delegate.Remove() keeping track of the count of
delegates.
Just use += and -= as shortcuts or Combine and Remove - and when it
returns null, that means the last subscriber has been removed. No need
to do explicit counting.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Sep 17 '08 #2
On Sep 17, 3:57*pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
That sounds feasible. In that case, the EventInfo would actually give
you everything you need - admittedly you'd be calling the
subscribe/unsubscribe bits by reflection (unless you created a delegate
to do that - very confusing!) but it would do the trick.
I guess I was originally thinking I wanted to avoid using reflection
due to the volume of events but since it would only be used for
subscribe / unsubscribe, and that won't happen that often, maybe it
wouldn't be such a bad solution. I always feel dirty using reflection
kind of like using IDispatch; I need to take a shower after using
either in code.

Sep 17 '08 #3

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

Similar topics

4
by: Martin Lucas-Smith | last post by:
Having re-read www.php.net/functions.arguments recently, the notion of passing arguments by reference to a function makes a lot more sense now. However, my question is: is there any difference in...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
12
by: Joel | last post by:
Hi all, Forgive me if I've expressed the subject line ill. What I'm trying to do is to call a c++ function given the following: a. A function name. This would be used to fetch a list of...
5
by: knocte | last post by:
Hello. I am a web developer very worried about "bloat code" and "languages mixture". So, since some time, I always try to avoid completely the use of javascript in XHTML/HTML files. This leads...
6
by: Max | last post by:
Last time I tried to explain this on another forum it didn't go too well, so I'll try my best and if you know what I'm talking about then please tell me how to do this. I have a class, inside I...
4
by: 63q2o4i02 | last post by:
Hi, I'm writing a hand-written recursive decent parser for SPICE syntax parsing. In one case I have one function that handles a bunch of similar cases (you pass the name and the number of...
10
by: Janus | last post by:
Hi, Is there a way to pass arguments to the callback function used inside an addEventListener? I see that I can only list the name of the callback function. For eg, I use this: var...
2
by: PatrickMinnesota | last post by:
I've been reading the docs and looking for an answer and seem stuck. I'm either not looking in the right places or not understanding what I'm reading. I have a bunch of functions. I want to put...
1
by: swethak | last post by:
Hi, I am desiging the calendar application for that purpose i used the below code. But it is for only displys calendar. And also i want to add the events to calendar. In that code displys the...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.