using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Runtime.Remoting.Messaging;
namespace ThreadAsync
{
class Program
{
static void sleep(ref int r)
{
DateTime start = DateTime.Now;
// r = Int32.Parse(o.ToString());
Thread.Sleep(r * 1000);
DateTime stop = DateTime.Now;
TimeSpan timeSpan = stop - start;
r = (int)timeSpan.TotalMilliseconds;
// Console.WriteLine("it sleeps for {0} seconds", r);
}
delegate void AsnycDelegate(ref int r);
static void Main(string[] args)
{
AsnycDelegate d = new AsnycDelegate(sleep);
for (int i = 0; i < 5; i++)
{
Random random = new Random();
int r = random.Next(1, 3);
d.BeginInvoke(ref r, new AsyncCallback(done), DateTime.Now);
}
Console.Read();
Console.WriteLine("main exit");
}
static void done(IAsyncResult ar)
{
Console.WriteLine("done");
AsyncResult result = (AsyncResult)ar;
DateTime param = (DateTime)result.AsyncState;
AsnycDelegate handler = (AsnycDelegate)result.AsyncDelegate;
int r = 0;
handler.EndInvoke(ref r, result);
Console.WriteLine("sleep {0} seconds", r);
Console.WriteLine("done exit");
}
}
}