The delegate must have only one target??

G

Guest

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;;
 
J

Jon Skeet [C# MVP]

Ed A said:
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.
 
G

Guest

Thanks Jon.


Ed;;

Jon Skeet said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top