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"); } }
Language Basics
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); } }
Local variables used in an anonymous method are called outer variables.
using System; public delegate void DelegateClass(out int arg); public class Starter { public static void Main() { DelegateClass del = MethodA(); int var; del(out var); Console.WriteLine(var); } public static DelegateClass MethodA() { int local = 0; return delegate(out int arg) { arg = ++local; }; } }