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

The delegate must have only one target??


Hi all:

My class has one event that gets fired on a timer, typically only one
function consumes it on the receiving end.

// Source Class
public delegate void DataHandler(ArrayList aList);
public event DataHandler DataEvent;

...
...

DataEvent.BeginInvoke(new ArrayList(),null,null);
// The Sink Class
mySourceObj.DataEvent += new DataHandler(OnData);
Everything works just fine but when I try to add another consumer to the
same event (mySourceObj.DataEvent += new DataHandler(OnData2);) I get this
runtime exception:
"The delegate must have only one target"

I'm assuming that I need to implement a MulticastDelegate but I can't find a
good example on how to do that. Could someone show me how to convert the
above code to using Multicast Delegates.
Thanks
Ed;;

Nov 17 '05 #1
2 14817
Ed A <Ed*@discussions.microsoft.com> wrote:
My class has one event that gets fired on a timer, typically only one
function consumes it on the receiving end.

// Source Class
public delegate void DataHandler(ArrayList aList);
public event DataHandler DataEvent;

..
..

DataEvent.BeginInvoke(new ArrayList(),null,null);

// The Sink Class
mySourceObj.DataEvent += new DataHandler(OnData);

Everything works just fine but when I try to add another consumer to the
same event (mySourceObj.DataEvent += new DataHandler(OnData2);) I get this
runtime exception:
"The delegate must have only one target"

I'm assuming that I need to implement a MulticastDelegate but I can't find a
good example on how to do that. Could someone show me how to convert the
above code to using Multicast Delegates.


You can only call BeginInvoke on a delegate which has a single target
invocation.

How do you want the threading to work? Do you have to run each
invocation synchronously, but run the whole thing asynchronously with
respect to the calling thread, or could you run each invocation
asynchronously?

If it's the former, just run a single threadpool work item which calls
the delegate synchronously. If it's the latter, get the invocation list
with Delegate.GetInvocationList and call BeginInvoke on element of the
list in turn.

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

Thanks Jon.
Ed;;

"Jon Skeet [C# MVP]" wrote:
Ed A <Ed*@discussions.microsoft.com> wrote:
My class has one event that gets fired on a timer, typically only one
function consumes it on the receiving end.

// Source Class
public delegate void DataHandler(ArrayList aList);
public event DataHandler DataEvent;

..
..

DataEvent.BeginInvoke(new ArrayList(),null,null);

// The Sink Class
mySourceObj.DataEvent += new DataHandler(OnData);

Everything works just fine but when I try to add another consumer to the
same event (mySourceObj.DataEvent += new DataHandler(OnData2);) I get this
runtime exception:
"The delegate must have only one target"

I'm assuming that I need to implement a MulticastDelegate but I can't find a
good example on how to do that. Could someone show me how to convert the
above code to using Multicast Delegates.


You can only call BeginInvoke on a delegate which has a single target
invocation.

How do you want the threading to work? Do you have to run each
invocation synchronously, but run the whole thing asynchronously with
respect to the calling thread, or could you run each invocation
asynchronously?

If it's the former, just run a single threadpool work item which calls
the delegate synchronously. If it's the latter, get the invocation list
with Delegate.GetInvocationList and call BeginInvoke on element of the
list in turn.

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

Nov 17 '05 #3

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

Similar topics

4
by: Tim Werth | last post by:
I am trying to use reflection to add an event handler for the RowUpdated event of the OracleDataAdapter object (ODP.NET), but the same thing can be said for SqlDataAdapter if you only use...
0
by: Undecided | last post by:
I have this routine in the ancestor class: protected ISynchronizeInvoke Target; protected void FireAsync(Delegate dlg, params object pList) { if (dlg != null) { Target.BeginInvoke(dlg,...
2
by: Russell Hind | last post by:
I have a delegate which I use to store a current 'state' function (for a statemachine inside a form). __delegate void State_t(const Message_c& Message); I assign to it such as m_State = new...
4
by: CJ Taylor | last post by:
What does this mean? This method creates delegates for static methods only. An instance method is a method that is associated with an instance of a class; a static method is a method that is...
1
by: CJ Taylor | last post by:
Alright, I probably wont be able to get this question answered, but maybe someone has even more experience than me with delegates... Wow... that sounded fecisious. =) So, I'm trying to...
1
by: Quimbly | last post by:
I'm having some problems comparing delegates. In all sample projects I create, I can't get the problem to occur, but there is definitely a problem with my production code. I can't give all the...
11
by: matsi.inc | last post by:
I am looking to make something like a delegate that i can use in my projects but am having a hard time getting started. The behavior I am most interested in is how a delegate changes it's Invoke...
3
by: puzzlecracker | last post by:
A cut-and-paste from the Jon's book, with a question to follow: Utter garbage! (Or not, as the case may be…)—It’s worth being aware that a delegate instance will prevent its target from being...
10
by: vcquestions | last post by:
Hi. Is there way to have a function pointer to a delegate in c++/cli that would allow me to pass delegates with the same signatures as parameters to a method? I'm working with managed code. ...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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:
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
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,...
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...

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.