using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace AsyncDelegateExam
{
delegate int TakeAWhileDelegate(int data,int ms);
class Program
{
static void Main(string[] args)
{
TakeAWhileDelegate del = TakeAWhile;
IAsyncResult ar = del.BeginInvoke(1, 1000, null, null);
while (true)
{
Console.WriteLine(".");
if (ar.AsyncWaitHandle.WaitOne(50,false))
{
Console.WriteLine("Can Get Result now!");
break;
}
}
int result = del.EndInvoke(ar);
Console.WriteLine("result:{0}", result);
Console.Read();
}
static int TakeAWhile(int data,int ms)
{
Console.WriteLine("TakeAWhile Started!");
Thread.Sleep(ms);
Console.WriteLine("TakeAWhile Completed!");
return ++data;
}
}
}
请发表评论