![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace Async2
{
public class MyAsync
{
public delegate string LengthyProcedureAsyncStub(int millisenconds);
public string LengthyProcedure(int milliseconds)
{
System.Threading.Thread.Sleep(milliseconds);
return "success";
}
public IAsyncResult BeginLengthyProcedure(int millseconds, AsyncCallback callback)
{
LengthyProcedureAsyncStub stub = new LengthyProcedureAsyncStub(LengthyProcedure);
IAsyncResult result = stub.BeginInvoke(millseconds, callback, stub);
return result;
}
public string EndLengthyProcedure(IAsyncResult result)
{
LengthyProcedureAsyncStub stub = (LengthyProcedureAsyncStub)result.AsyncState;
string res = stub.EndInvoke(result);
return res;
}
}
class Program
{
static void Main(string[] args)
{
MyAsync ma = new MyAsync();
IAsyncResult result = ma.BeginLengthyProcedure(5000, new AsyncCallback(showmessage));
Console.WriteLine(ma.EndLengthyProcedure(result));
Console.WriteLine("main");
Console.Read();
}
static void showmessage(IAsyncResult result)
{
Console.WriteLine("showmessage");
}
}
}