1.创建线程的一种简单方法是定义一个委托,异步调用它。一种技术是投票,检查委托是否完成了任务。
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
namespace ConsolTread
{
class Program
{
//委托使用线程池来完成异步任务C#高级编程中代码
static int TakeAAwhile(int data,int ms)
{
Console.WriteLine("takeaawhile started");
Thread.Sleep(ms);
Console.WriteLine("takeaawhile completed");
return ++data;
}
//创建委托
public delegate int TakeAAwhileDelegate(int data, int ms);
static void Main(string[] args)
{
TakeAAwhileDelegate dl = TakeAAwhile;
//IAsyncResult可以获得委托信息,并验证委托是否完成了任务。
IAsyncResult ar=dl.BeginInvoke(1,700,null,null);
while(!ar.IsCompleted)
{
Console.WriteLine(".");
Thread.Sleep(50);
}
int result = dl.EndInvoke(ar);
Console.WriteLine("result:{0}", result);
}
}
}
第二种是使用与IAsyncResult相关的等待句柄
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;
namespace asyThread
{
class Program
{
//委托使用线程池来完成异步任务C#高级编程中代码
static int TakeAAwhile(int data, int ms)
{
Console.WriteLine("takeaawhile started");
Thread.Sleep(ms);
Console.WriteLine("takeaawhile completed");
return ++data;
}
//创建委托
public delegate int TakeAAwhileDelegate(int data, int ms);
static void Main(string[] args)
{
//等待句柄
TakeAAwhileDelegate dl = TakeAAwhile;
IAsyncResult ar = dl.BeginInvoke(1, 3000, null, null);
while(true)
{
Console.Write(".");
if (ar.AsyncWaitHandle.WaitOne(50,false))
{
Console.WriteLine("Can get the result");
break;
}
}
int result = dl.EndInvoke(ar);
Console.WriteLine("result:{0}", result);
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;
namespace asyThread
{
class Program
{
//委托使用线程池来完成异步任务C#高级编程中代码
static int TakeAAwhile(int data, int ms)
{
Console.WriteLine("takeaawhile started");
Thread.Sleep(ms);
Console.WriteLine("takeaawhile completed");
return ++data;
}
//创建委托
public delegate int TakeAAwhileDelegate(int data, int ms);
static void Main(string[] args)
{
//等待句柄
TakeAAwhileDelegate dl = TakeAAwhile;
IAsyncResult ar = dl.BeginInvoke(1, 3000, null, null);
while(true)
{
Console.Write(".");
if (ar.AsyncWaitHandle.WaitOne(50,false))
{
Console.WriteLine("Can get the result");
break;
}
}
int result = dl.EndInvoke(ar);
Console.WriteLine("result:{0}", result);
}
}
}
第三种是使用异步回调方法
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Diagnostics;
namespace Wrox.ProCSharp.Threading
{
class Program
{
static int TakesAWhile(int data, int ms)
{
Console.WriteLine("TakesAWhile started");
Thread.Sleep(ms);
Console.WriteLine("TakesAWhile completed");
return ++data;
}
public delegate int TakesAWhileDelegate(int data, int ms);
static void Main()
{
TakesAWhileDelegate d1 = TakesAWhile;
d1.BeginInvoke(1, 3000,
ar =>
{
int result = d1.EndInvoke(ar);
Console.WriteLine("result: {0}", result);
},
null);
for (int i = 0; i < 100; i++)
{
Console.Write(".");
Thread.Sleep(50);
}
}
static void TakesAWhileCompleted(IAsyncResult ar)
{
if (ar == null) throw new ArgumentNullException("ar");
TakesAWhileDelegate d1 = ar.AsyncState as TakesAWhileDelegate;
Trace.Assert(d1 != null, "Invalid object type");
int result = d1.EndInvoke(ar);
Console.WriteLine("result: {0}", result);
}
}
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Diagnostics;
namespace Wrox.ProCSharp.Threading
{
class Program
{
static int TakesAWhile(int data, int ms)
{
Console.WriteLine("TakesAWhile started");
Thread.Sleep(ms);
Console.WriteLine("TakesAWhile completed");
return ++data;
}
public delegate int TakesAWhileDelegate(int data, int ms);
static void Main()
{
TakesAWhileDelegate d1 = TakesAWhile;
d1.BeginInvoke(1, 3000,
ar =>
{
int result = d1.EndInvoke(ar);
Console.WriteLine("result: {0}", result);
},
null);
for (int i = 0; i < 100; i++)
{
Console.Write(".");
Thread.Sleep(50);
}
}
static void TakesAWhileCompleted(IAsyncResult ar)
{
if (ar == null) throw new ArgumentNullException("ar");
TakesAWhileDelegate d1 = ar.AsyncState as TakesAWhileDelegate;
Trace.Assert(d1 != null, "Invalid object type");
int result = d1.EndInvoke(ar);
Console.WriteLine("result: {0}", result);
}
}
}