通过委托实现异步回调
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
funHandle theHandle = fun;
IAsyncResult iResult = theHandle.BeginInvoke(5000, null, null);
Console.WriteLine("message from main");
int timestart = Environment.TickCount;
while (true && Environment.TickCount - timestart < 10000)
{
if (iResult.IsCompleted)
{
string str = theHandle.EndInvoke(iResult);
Console.WriteLine(str);
break;
}
System.Threading.Thread.Sleep(300);
}
Console.ReadLine();
}
static string fun(int sleeptime)
{
Console.WriteLine("msssage from fun1");
System.Threading.Thread.Sleep(sleeptime);
Console.WriteLine("message from fun2");
return "returnmsg";
}
delegate string funHandle(int sleeptime);
}
}