question

la07k-4914 avatar image
0 Votes"
la07k-4914 asked Viorel-1 edited

How Multicast delegates invoking associated methods?

Hi,

I have created a delegate and I associated with multiple methods to that delegate. My requirement is on one invoking I have to trigger all the associated methods.
Here i saw all the methods are invoked but its not finished on the same time. Its running like a synchronous manner(because its running in a single threaded application).
Actually how the multicast delegate is invoking the associated methods in the background?

     Display ddd=new Display(EventHandler);
     ddd += EventHandler1;
     ddd += EventHandler2;
     ddd += EventHandler3;
     ddd += EventHandler4;
     ddd += EventHandler5;

.
.
.
.


     ddd += EventHandlerXXX;
     private static void EventHandler()
     {
         Console.WriteLine(DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss:ffff"));
     }
     private static void EventHandler1()
     {
         Console.WriteLine(DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss:ffff"));
     }
     private static void EventHandlerXXXX()
     {
         Console.WriteLine(DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss:ffff"));
     }


One scenario have a shopping site, multiple users are subscribed for the availability of a particular product. Once item is available I can notify using delegates/events. But how I can ensure everyone will notified exactly on the same time?

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Viorel-1 avatar image
0 Votes"
Viorel-1 answered Viorel-1 edited

I think that the handlers are called sequentially, using a loop.

To deal with the same time value in all of the handlers, consider a parameter, something like this:

 DateTime now = DateTime.Now;
 ddd( now );

Adjust the definition of delegate and event handlers to take an input parameter, for example:

 delegate void Display( DateTime d );
    
 private static void EventHandler( DateTime d) { Console.WriteLine(d); }



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.