(a)
public delegate bool aCallBackType( String myArg1, ref int myArg2 );
public class Worker1
{
  public bool WriteLine( String myArg1, ref int myArg2 )
  {
    System.Console.WriteLine( myArg1, myArg2++ );
    return true;
  }
}
public class Worker2
{
  public bool FindSubString( String myArg1, ref int myArg2 )
  {
    return ( myArg1.IndexOf( myArg2--.ToString() ) >= 0 );
  }
}
public class MyApp
{
  public static void Main()
  {
    Worker1 w1  = new Worker1();
    Worker2 w2  = new Worker2();
    aCallBackType writeLine  = new aCallBackType( w1.WriteLine );
    aCallBackType findSubStr = new aCallBackType( w2.FindSubString );
    int aCounter = 1;
    bool bWriteLine
        = writeLine(  "Delegate invocation {0}", ref aCounter );
    bool bFindSubStr
        = findSubStr( "Can't find substring...",  ref aCounter );
  }
}

(b)
public class MyApp
{
  public static void Main()
  {
    Worker1 w1  = new Worker1();
    Worker2 w2  = new Worker2();
    aCallBackType writeLine  = new aCallBackType( w1.WriteLine );
    aCallBackType findSubStr = new aCallBackType( w2.FindSubString );
    int aCounter = 1;
    IAsyncResult writeLineResult
         = writeLine.BeginInvoke( "Delegate invocation {0}", ref aCounter, null, null );
    IAsyncResult findSubStrResult
         = findSubStr.BeginInvoke( "Can't find substring...", ref aCounter, null, null );
    while ( ( ! writeLineResult.IsCompleted  ) &&
            ( ! findSubStrResult.IsCompleted ) )
    {
      System.Thread.Sleep(0);
    }
    bool bWriteLine
         = writeLine.EndInvoke( ref aCounter, writeLineResult );
    bool bFindSubStr
         = findSubStr.EndInvoke( ref aCounter, findSubStrResult );
  }
}

Example 1.

Back to Article