using System;
using System.Threading;
namespace DelegateThread
{
class MainThread
{
static void Main(string[] args)
{
DelegateThread thisThread;
DelegateThreadContent thisClass = new DelegateThreadContent();
thisThread = thisClass.DelegateThreadMethodContent;
//Begin the thread
//Call back the method "CallBackMethod"
//Send the object "thisThread" to the property "AsyncState"
IAsyncResult thisResult=thisThread.BeginInvoke(3000,DelegateThreadContent.CallBackMethod,thisThread);
//Execute the main thread
while (!thisResult.IsCompleted)
{
//Set 50 as the time out time.
//If thread haven't completed after 50, return false.
if(!thisResult.AsyncWaitHandle.WaitOne(50,false))
{
Console.Write(".");
}
}
Console.ReadKey();
}
}
delegate void DelegateThread(int time);
class DelegateThreadContent
{
public static void CallBackMethod(IAsyncResult asyncResult)
{
DelegateThread currentDelegateThread = asyncResult.AsyncState as DelegateThread;
Console.WriteLine("Type Of Current Thread:{0}",currentDelegateThread.GetType());
//We can get the thread result by method EndInvoke
currentDelegateThread.EndInvoke(asyncResult);
}
public void DelegateThreadMethodContent(int time)
{
Console.WriteLine("Delegate Thread Started...");
Thread.Sleep(time);
Console.WriteLine("/nDelegate Thread Ended...");
}
}
}