How do i pass method to asynchronous delegate invocation, so that it could be explicitly processed asynchronously on second processor? Thanks in advance.
ADDED: I had problem with encoding internet so sorry for upper case. I have tried:
BTW Have no javascript enabled in browser so I can't add comments to this post. Will be editing this post.
using System;
using System.Threading;
using System.Runtime.Remoting.Messaging;
namespace Delegate
{
public class SampleClass
{
public static bool SampleMethod()
{
Console.WriteLine("Inside sample method ...");
return true;
}
public static void SampleThreadMethod()
{
while (true)
{
Console.WriteLine("Inside sample thread method ...");
Thread.Sleep(500);
}
}
}
public delegate bool SampleMethodCaller();
public class DelegateSample
{
ManualResetEvent waiter;
public void CallBackMethodForDelegate(IAsyncResult result)
{
SampleMethodCaller smd = (SampleMethodCaller)((AsyncResult)result).AsyncDelegate;
bool returnValue = smd.EndInvoke(result);
Console.WriteLine("Callback Result:- {0}", returnValue);
waiter.Set();
}
public void CallDelegateAndWait()
{
Console.WriteLine("Sample 1: Calling delegate using BeginInvoke and waiting for it to complete");
SampleMethodCaller smd = new SampleMethodCaller(SampleClass.SampleMethod);
IAsyncResult result = smd.BeginInvoke(null, null);
Console.WriteLine("After invoking delegate no callback...");
bool returnValue = false;
returnValue = smd.EndInvoke(result);
Console.WriteLine("Wait Result:- {0}", returnValue);
}
public void CallDelegateUsingCallBack()
{
Console.WriteLine("Sample 2: Calling delegate using BeginInvoke and waiting on call back");
waiter = new ManualResetEvent(false);
SampleMethodCaller smd = new SampleMethodCaller(SampleClass.SampleMethod);
IAsyncResult result = smd.BeginInvoke(CallBackMethodForDelegate, null);
Console.WriteLine("After invoking delegate in callback...");
waiter.WaitOne();
}
public void CallDelegateUsingInvoke()
{
Console.WriteLine("Sample 3: Calling delegate using Invoke");
SampleMethodCaller smd = new SampleMethodCaller(SampleClass.SampleMethod);
bool result = smd.Invoke();
Console.WriteLine("Invoke call Result:- {0}", result);
}
public void CallDelegate()
{
Console.WriteLine("Sample 4: Calling delegate straight forward");
SampleMethodCaller smd = new SampleMethodCaller(SampleClass.SampleMethod);
bool result = smd();
Console.WriteLine("Simple call Result:- {0}", result);
}
public void CallDelegateDynamicInvoke()
{
Console.WriteLine("Sample 5: Calling delegate using DynamicInvoke");
SampleMethodCaller smd = new SampleMethodCaller(SampleClass.SampleMethod);
bool result = (bool)smd.DynamicInvoke();
Console.WriteLine("Dynamic Invoke call Result:- {0}", result);
}
public void CallingDelegateOnAThread()
{
Console.WriteLine("Sample 6: Calling delegate on a thread");
Thread nt = new Thread(SampleClass.SampleThreadMethod);
nt.Start();
Thread.Sleep(2000);
Console.ReadLine();
nt.Abort();
}
public static void Main()
{
DelegateSample ds = new DelegateSample();
ds.CallDelegateAndWait();
Console.WriteLine(" -----------------");
ds.CallDelegateUsingCallBack();
Console.WriteLine(" -----------------");
ds.CallDelegateUsingInvoke();
Console.WriteLine(" -----------------");
ds.CallDelegate();
Console.WriteLine(" -----------------");
ds.CallDelegateDynamicInvoke();
Console.WriteLine(" -----------------");
ds.CallingDelegateOnAThread();
}
}
}