An event multicast demonstration

image_pdfimage_print

   

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/


// An event multicast demonstration. 
  
using System; 
 
// Declare a delegate for an event.  
delegate void MyEventHandler(); 
 
// Declare an event class. 
class MyEvent { 
  public event MyEventHandler SomeEvent; 
 
  // This is called to fire the event. 
  public void OnSomeEvent() { 
    if(SomeEvent != null) 
      SomeEvent(); 
  } 
} 
 
class X { 
  public void Xhandler() { 
    Console.WriteLine("Event received by X object"); 
  } 
} 
 
class Y { 
  public void Yhandler() { 
    Console.WriteLine("Event received by Y object"); 
  } 
} 
 
public class EventDemo1 { 
  static void handler() { 
    Console.WriteLine("Event received by EventDemo"); 
  } 
 
  public static void Main() {  
    MyEvent evt = new MyEvent(); 
    X xOb = new X(); 
    Y yOb = new Y(); 
 
    // Add handlers to the event list. 
    evt.SomeEvent += new MyEventHandler(handler); 
    evt.SomeEvent += new MyEventHandler(xOb.Xhandler); 
    evt.SomeEvent += new MyEventHandler(yOb.Yhandler); 
 
    // Fire the event. 
    evt.OnSomeEvent(); 
    Console.WriteLine(); 
 
    // Remove a handler. 
    evt.SomeEvent -= new MyEventHandler(xOb.Xhandler); 
    evt.OnSomeEvent(); 
  } 
}


           
          


A very simple event demonstration

image_pdfimage_print

   

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/


// A very simple event demonstration. 
  
using System; 
 
// Declare a delegate for an event.  
delegate void MyEventHandler(); 
 
// Declare an event class. 
class MyEvent { 
  public event MyEventHandler SomeEvent; 
 
  // This is called to fire the event. 
  public void OnSomeEvent() { 
    if(SomeEvent != null) 
      SomeEvent(); 
  } 
} 
 
public class EventDemo { 
  // An event handler. 
  static void handler() { 
    Console.WriteLine("Event occurred"); 
  } 
 
  public static void Main() {  
    MyEvent evt = new MyEvent(); 
 
    // Add handler() to the event list. 
    evt.SomeEvent += new MyEventHandler(handler); 
 
    // Fire the event. 
    evt.OnSomeEvent(); 
  } 
} 



           
          


What happens if an unhandled exception is raised in a multicast delegate?

image_pdfimage_print
   
 

using System;
using System.Threading;


public delegate void DelegateClass();

public class Starter {
    public static void Main() {
        try {
            DelegateClass del = MethodA;
            IAsyncResult ar = del.BeginInvoke(null, null);
            del.EndInvoke(ar);
        } catch (Exception except) {
            Console.WriteLine("Exception caught: "+ except.Message);
        }
    }

    public static void MethodA() {
        if (Thread.CurrentThread.IsThreadPoolThread == true) {
            Console.WriteLine("Running on a thread pool thread");
        } else {
            Console.WriteLine("Running on primary thread");
        }
        throw new Exception("failure");
    }
}

    


delegate BeginInvoke of IAsyncResult

image_pdfimage_print

using System;
using System.Threading;

class AsyncDelegatesBlocked
{
public static int Add(int op1, int op2, out int result)
{
Thread.Sleep(3000);
return (result = op1 + op2);
}
public delegate int AddDelegate(int op1, int op2,out int result);

static void Main()
{
int result;
AddDelegate add = new AddDelegate(Add);

IAsyncResult iAR = add.BeginInvoke(6, 42, out result,null, null);

for (int i = 0; i < 10; i++) { Thread.Sleep(200); Console.Write("."); } iAR.AsyncWaitHandle.WaitOne(); add.EndInvoke(out result, iAR); Console.WriteLine("[Main] The result is {0}", result); } } [/csharp]

Async Delegates Callback

image_pdfimage_print
   
 

using System;
using System.Threading;
   
class AsyncDelegatesCallback
{
    public static int Add(int op1, int op2, out int result)
    {
        Thread.Sleep(1000); 
        return (result = op1 + op2);
    }
    public delegate int AddDelegate(int op1, int op2,
        out int result);
   
    public static void AnnounceSum(IAsyncResult iar)
    {
        AddDelegate add = (AddDelegate)iar.AsyncState;
   
        int result;
        add.EndInvoke(out result, iar);
   
        Console.WriteLine("[AnnounceSum] The result is {0}", result);
    }
   
    static void Main()
    {
        int result;
        AddDelegate add = new AddDelegate(Add);
        add.BeginInvoke(6, 42, out result, new AsyncCallback(AnnounceSum), add);
   
        Thread.Sleep(1000); 
    }
}

    


Async Delegate Invocation

image_pdfimage_print
   
 

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

public delegate int BinaryOp(int x, int y);

class Program {
    static void Main(string[] args) {
        BinaryOp b = new BinaryOp(Add);
        IAsyncResult iftAR = b.BeginInvoke(10, 10, null, null);

        while (!iftAR.AsyncWaitHandle.WaitOne(2000, true)) {
            Console.WriteLine("Doing more work in Main()!");
        }

        int answer = b.EndInvoke(iftAR);
        Console.WriteLine("10 + 10 is {0}.", answer);
    }

    static int Add(int x, int y) {
        Console.WriteLine("Add() invoked on thread {0}.", Thread.CurrentThread.GetHashCode());
        Thread.Sleep(5000);
        return x + y;
    }
}