473,404 Members | 2,187 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,404 software developers and data experts.

Event Subscribers

Tim
Hi,

I have a module that acts as a publisher of events. The clients subscribe
for the events using the '+=' operator. Instead, I would like the clients to
call a method like "RegisterForXEvent" passing the required information.
Inside that method, I would like to add the client to the list of
subscribers. Is it possible to implement the above. If so, how do I do it?
What kinda information should the client pass to the Event publisher?
Dec 16 '05 #1
6 1690
Well, technically yes it would be possible, by making the RegisterForXEvent
method accept a delegate of the appropriate type... but the big question
would be "why?". The standard event mechanism is well understood, and
(frankly) works very well in most scenarios.

If (inside the standard event model) you want to do something a bit more
bespoke when registering, you can do this by providing your own "add" and
"remove" implementations (that are the += and -= operators):

private event EventHandler _myEvent;
public event EventHandler MyEvent {
add {
if (SomeTest()) // check if the subscriber is permitted
throw new Exception(@"Talk to the hand, 'cos the face
ain't listening");
_myEvent += value;
}
remove { _myEvent -= value; }
}
protected void OnMyEvent() {
if (_myEvent != null) _myEvent(this, new EventArgs());
}

If you are trying to do something more complicated, then there isn't enough
information in your post to answer the "what kinda information" question.

Marc

"Tim" <Ti*@discussions.microsoft.com> wrote in message
news:2C**********************************@microsof t.com...
Hi,

I have a module that acts as a publisher of events. The clients
subscribe
for the events using the '+=' operator. Instead, I would like the clients
to
call a method like "RegisterForXEvent" passing the required information.
Inside that method, I would like to add the client to the list of
subscribers. Is it possible to implement the above. If so, how do I do it?
What kinda information should the client pass to the Event publisher?

Dec 16 '05 #2
Tim
Hi,

Thanks for the response - Marc. Regarding the question "Why" - The
clients could be COM/PInvoke clients or managed clients. Instead of
describing how they need to subscribe to the events, it would be much simpler
to ask them to make a method call. Well, I am not sure about the complexities
that may be involved in the process - which I am just trying to figure out.

Regarding the 'Add' implementation, what information does the caller
have to pass to the Event publisher? Will that suit my requirement - of
having to support diff kinds of clients.


"Marc Gravell" wrote:
Well, technically yes it would be possible, by making the RegisterForXEvent
method accept a delegate of the appropriate type... but the big question
would be "why?". The standard event mechanism is well understood, and
(frankly) works very well in most scenarios.

If (inside the standard event model) you want to do something a bit more
bespoke when registering, you can do this by providing your own "add" and
"remove" implementations (that are the += and -= operators):

private event EventHandler _myEvent;
public event EventHandler MyEvent {
add {
if (SomeTest()) // check if the subscriber is permitted
throw new Exception(@"Talk to the hand, 'cos the face
ain't listening");
_myEvent += value;
}
remove { _myEvent -= value; }
}
protected void OnMyEvent() {
if (_myEvent != null) _myEvent(this, new EventArgs());
}

If you are trying to do something more complicated, then there isn't enough
information in your post to answer the "what kinda information" question.

Marc

"Tim" <Ti*@discussions.microsoft.com> wrote in message
news:2C**********************************@microsof t.com...
Hi,

I have a module that acts as a publisher of events. The clients
subscribe
for the events using the '+=' operator. Instead, I would like the clients
to
call a method like "RegisterForXEvent" passing the required information.
Inside that method, I would like to add the client to the list of
subscribers. Is it possible to implement the above. If so, how do I do it?
What kinda information should the client pass to the Event publisher?


Dec 16 '05 #3
Fair enough answer...

I'm not too familiar with hooking up COM (or other unmanaged) clients to
..Net events (or some similar solution), so now that we all properly
understand the question, I'll duck out (I don't want to confuse things).

Marc
"Tim" <Ti*@discussions.microsoft.com> wrote in message
news:51**********************************@microsof t.com...
Hi,

Thanks for the response - Marc. Regarding the question "Why" - The
clients could be COM/PInvoke clients or managed clients. Instead of
describing how they need to subscribe to the events, it would be much
simpler
to ask them to make a method call. Well, I am not sure about the
complexities
that may be involved in the process - which I am just trying to figure
out.

Regarding the 'Add' implementation, what information does the caller
have to pass to the Event publisher? Will that suit my requirement - of
having to support diff kinds of clients.


"Marc Gravell" wrote:
Well, technically yes it would be possible, by making the
RegisterForXEvent
method accept a delegate of the appropriate type... but the big question
would be "why?". The standard event mechanism is well understood, and
(frankly) works very well in most scenarios.

If (inside the standard event model) you want to do something a bit more
bespoke when registering, you can do this by providing your own "add" and
"remove" implementations (that are the += and -= operators):

private event EventHandler _myEvent;
public event EventHandler MyEvent {
add {
if (SomeTest()) // check if the subscriber is permitted
throw new Exception(@"Talk to the hand, 'cos the face
ain't listening");
_myEvent += value;
}
remove { _myEvent -= value; }
}
protected void OnMyEvent() {
if (_myEvent != null) _myEvent(this, new EventArgs());
}

If you are trying to do something more complicated, then there isn't
enough
information in your post to answer the "what kinda information" question.

Marc

"Tim" <Ti*@discussions.microsoft.com> wrote in message
news:2C**********************************@microsof t.com...
> Hi,
>
> I have a module that acts as a publisher of events. The clients
> subscribe
> for the events using the '+=' operator. Instead, I would like the
> clients
> to
> call a method like "RegisterForXEvent" passing the required
> information.
> Inside that method, I would like to add the client to the list of
> subscribers. Is it possible to implement the above. If so, how do I do
> it?
> What kinda information should the client pass to the Event publisher?


Dec 16 '05 #4
Tim wrote:
Hi,

Thanks for the response - Marc. Regarding the question "Why" - The
clients could be COM/PInvoke clients or managed clients. Instead of
describing how they need to subscribe to the events, it would be much simpler
to ask them to make a method call. Well, I am not sure about the complexities
that may be involved in the process - which I am just trying to figure out.

Regarding the 'Add' implementation, what information does the caller
have to pass to the Event publisher? Will that suit my requirement - of
having to support diff kinds of clients.
If you mean that you client is the actual object that the method the
delegate represenst is called on, you can get that from the delegate
using its Target property.
HTH,
Andy

"Marc Gravell" wrote:

Well, technically yes it would be possible, by making the RegisterForXEvent
method accept a delegate of the appropriate type... but the big question
would be "why?". The standard event mechanism is well understood, and
(frankly) works very well in most scenarios.

If (inside the standard event model) you want to do something a bit more
bespoke when registering, you can do this by providing your own "add" and
"remove" implementations (that are the += and -= operators):

private event EventHandler _myEvent;
public event EventHandler MyEvent {
add {
if (SomeTest()) // check if the subscriber is permitted
throw new Exception(@"Talk to the hand, 'cos the face
ain't listening");
_myEvent += value;
}
remove { _myEvent -= value; }
}
protected void OnMyEvent() {
if (_myEvent != null) _myEvent(this, new EventArgs());
}

If you are trying to do something more complicated, then there isn't enough
information in your post to answer the "what kinda information" question.

Marc

"Tim" <Ti*@discussions.microsoft.com> wrote in message
news:2C**********************************@micros oft.com...
Hi,

I have a module that acts as a publisher of events. The clients
subscribe
for the events using the '+=' operator. Instead, I would like the clients
to
call a method like "RegisterForXEvent" passing the required information.
Inside that method, I would like to add the client to the list of
subscribers. Is it possible to implement the above. If so, how do I do it?
What kinda information should the client pass to the Event publisher?


Dec 17 '05 #5
Tim wrote:
Hi,

Thanks for the response - Marc. Regarding the question "Why" - The
clients could be COM/PInvoke clients or managed clients. Instead of
describing how they need to subscribe to the events, it would be much simpler
to ask them to make a method call. Well, I am not sure about the complexities
that may be involved in the process - which I am just trying to figure out.

Regarding the 'Add' implementation, what information does the caller
have to pass to the Event publisher? Will that suit my requirement - of
having to support diff kinds of clients.
If you mean that you client is the actual object that the method the
delegate represenst is called on, you can get that from the delegate
using its Target property.
HTH,
Andy
"Marc Gravell" wrote:

Well, technically yes it would be possible, by making the RegisterForXEvent
method accept a delegate of the appropriate type... but the big question
would be "why?". The standard event mechanism is well understood, and
(frankly) works very well in most scenarios.

If (inside the standard event model) you want to do something a bit more
bespoke when registering, you can do this by providing your own "add" and
"remove" implementations (that are the += and -= operators):

private event EventHandler _myEvent;
public event EventHandler MyEvent {
add {
if (SomeTest()) // check if the subscriber is permitted
throw new Exception(@"Talk to the hand, 'cos the face
ain't listening");
_myEvent += value;
}
remove { _myEvent -= value; }
}
protected void OnMyEvent() {
if (_myEvent != null) _myEvent(this, new EventArgs());
}

If you are trying to do something more complicated, then there isn't enough
information in your post to answer the "what kinda information" question.

Marc

"Tim" <Ti*@discussions.microsoft.com> wrote in message
news:2C**********************************@micros oft.com...
Hi,

I have a module that acts as a publisher of events. The clients
subscribe
for the events using the '+=' operator. Instead, I would like the clients
to
call a method like "RegisterForXEvent" passing the required information.
Inside that method, I would like to add the client to the list of
subscribers. Is it possible to implement the above. If so, how do I do it?
What kinda information should the client pass to the Event publisher?


Dec 17 '05 #6
Tim
Hi,

Actually, I am interested in knowing if there is a way for Clients to
subscribe for events by making a method call. If so, what is the information
that needs to be provided - i.e. the signature of the method that should be
exposed by the event publisher.

"Andreas Mueller" wrote:
Tim wrote:
Hi,

Thanks for the response - Marc. Regarding the question "Why" - The
clients could be COM/PInvoke clients or managed clients. Instead of
describing how they need to subscribe to the events, it would be much simpler
to ask them to make a method call. Well, I am not sure about the complexities
that may be involved in the process - which I am just trying to figure out.

Regarding the 'Add' implementation, what information does the caller
have to pass to the Event publisher? Will that suit my requirement - of
having to support diff kinds of clients.

If you mean that you client is the actual object that the method the
delegate represenst is called on, you can get that from the delegate
using its Target property.
HTH,
Andy

"Marc Gravell" wrote:

Well, technically yes it would be possible, by making the RegisterForXEvent
method accept a delegate of the appropriate type... but the big question
would be "why?". The standard event mechanism is well understood, and
(frankly) works very well in most scenarios.

If (inside the standard event model) you want to do something a bit more
bespoke when registering, you can do this by providing your own "add" and
"remove" implementations (that are the += and -= operators):

private event EventHandler _myEvent;
public event EventHandler MyEvent {
add {
if (SomeTest()) // check if the subscriber is permitted
throw new Exception(@"Talk to the hand, 'cos the face
ain't listening");
_myEvent += value;
}
remove { _myEvent -= value; }
}
protected void OnMyEvent() {
if (_myEvent != null) _myEvent(this, new EventArgs());
}

If you are trying to do something more complicated, then there isn't enough
information in your post to answer the "what kinda information" question.

Marc

"Tim" <Ti*@discussions.microsoft.com> wrote in message
news:2C**********************************@micros oft.com...

Hi,

I have a module that acts as a publisher of events. The clients
subscribe
for the events using the '+=' operator. Instead, I would like the clients
to
call a method like "RegisterForXEvent" passing the required information.
Inside that method, I would like to add the client to the list of
subscribers. Is it possible to implement the above. If so, how do I do it?
What kinda information should the client pass to the Event publisher?

Dec 19 '05 #7

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

Similar topics

2
by: Pavils Jurjans | last post by:
Hello, I wanted to propose a small class that would help to overcome the feature that's missing in MSIE. I'd like to get some feedback from people and, perhaps, improvements in code/other ideas:...
4
by: Greg Patrick | last post by:
Let's say I have a Form and I create a delegate (MyDelegate) and an event (SomethingHappened) for that form. Now other classes add their delegate to the event, e.g. theForm.SomethingHappened +=...
1
by: alexis rzewski | last post by:
I have a design in place for a .NET app coded in C# in which the business object drives the appearence of the UI and when the business object experiences a change, it notifies the various UIs via...
6
by: Peter M. | last post by:
Hi all, If an event has multiple subscribers, is it possible to cancel the invocation of event handlers from an event handler? Or to be more specific: I'm subscribing to the ColumnChanging...
4
by: sloan | last post by:
I"m trying to figure out what concept I'm missing here, or if its not a good idea .. or what. Here is my example.. code is below. I have an employee class. It has an event that can be raised....
6
by: sjoshi | last post by:
I have a derived class OraBackup which has a method that calls stored procedure on Oracledb to get status of backup job. Now the base class publishes an event like this: public delegate void...
1
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi Is there a way to get a list of Subscribers to an Event ??? Thanks
3
by: Smithers | last post by:
In consideration of the brief sample code at the following link... http://msdn2.microsoft.com/en-us/library/system.componentmodel.canceleventargs.cancel.aspx .... when we set e.Cancel = true,...
4
by: =?Utf-8?B?TmF2YW5lZXRoLksuTg==?= | last post by:
Hi all, Recently I found an interesting question on C# forums about clearing event handlers of an event. I tried to give it a solution, but failed. I am interested to know how you guys take...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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:
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.