/* 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(); } }
Author: coder
What happens if an unhandled exception is raised in a multicast delegate?
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
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
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
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; } }
Associating the delegate with an anonymous method.
delegate void Printer(string s); class TestClass { static void Main() { Printer p = delegate(string j) { System.Console.WriteLine(j); }; p("this is a test."); } static void DoWork(string k) { System.Console.WriteLine(k); } }
Anonymous methods can be assigned a signature
using System; using System.Threading; public delegate int DelegateClass(out int param); public class Starter { public static void Main() { int var; DelegateClass del = delegate(out int inner) { inner = 12; Console.WriteLine("Running anonymous method"); return inner; }; del(out var); Console.WriteLine("Var is {0}", var); } }