using
System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace MyThread
{
class Program
{
private static int newTask( int ms)
{
Console.WriteLine( " 任务开始 " );
Thread.Sleep(ms);
Random random = new Random();
int n = random.Next( 10000 );
Console.WriteLine( " 任务完成 " );
return n;
}
private delegate int NewTaskDelegate( int ms);
static void Main( string [] args)
{
NewTaskDelegate task = newTask;
IAsyncResult asyncResult = task.BeginInvoke( 2000 , null , null );
while (!asyncResult.AsyncWaitHandle.WaitOne(100, false))
{
Console.Write("*");
}
// EndInvoke方法将被阻塞2秒
int result = task.EndInvoke(asyncResult);
Console.WriteLine(result);
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace MyThread
{
class Program
{
private static int newTask( int ms)
{
Console.WriteLine( " 任务开始 " );
Thread.Sleep(ms);
Random random = new Random();
int n = random.Next( 10000 );
Console.WriteLine( " 任务完成 " );
return n;
}
private delegate int NewTaskDelegate( int ms);
static void Main( string [] args)
{
NewTaskDelegate task = newTask;
IAsyncResult asyncResult = task.BeginInvoke( 2000 , null , null );
while (!asyncResult.AsyncWaitHandle.WaitOne(100, false))
{
Console.Write("*");
}
// EndInvoke方法将被阻塞2秒
int result = task.EndInvoke(asyncResult);
Console.WriteLine(result);
}
}
}