以下资料来自红皮书的摘录,以及网络的查询。本笔记最终是为了完成c# winform程序的进度条更新功能。
C#高级编程 线程一章 有用的资料摘录
一 异步委托
创建线程的一种简单方式是定义一个委托,异步调用它。委托是方法的类型安全的引用。
演示委托的异步特性,例子:
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);
以下通过不同的技术异步调用委托:投票,等待句柄,异步回调
投票方式:通过Delegate的BeginInvoke()方法的返回类型IAsyncResult.IAsyncResult中有个属性IsCompleted, 可以验证委托是否完成了任务.
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()
{
// synchronous
// TakesAWhile(1, 3000);
TakesAWhileDelegate d1 = TakesAWhile;
// polling
IAsyncResult ar = d1.BeginInvoke(1, 3000, null, null);
while (!ar.IsCompleted)
{
// doing something else
Console.Write(".");
Thread.Sleep(50);
}
int result = d1.EndInvoke(ar);
Console.WriteLine("result: {0}", result);
}
}
}
等待句柄方式:等待异步委托的结果的另一种方式是使用与IAsyncResult相关的等待句柄.例如:IAsyncResult.AsyncWaitHandle.WaitOne(50, false) AsyncWaitHandle属性可以访问等待句柄.这个属性返回一个WaitHandle类型的对象.
//wait handle
IAsyncResult ar = d1.BeginInvoke(1, 3000, null, null);
while (true)
{
Console.Write(".");
if (ar.AsyncWaitHandle.WaitOne(50, false))
{
Console.WriteLine("Can get the result now");
break;
}
}
int result = d1.EndInvoke(ar);
Console.WriteLine("result: {0}", result);
异步回调方式:d1.BeginInvoke(1, 3000, TakesAWhileCompleted, d1); 第三个参数需要传送一个满足AsyncCallback委托,相同签名的方法 具有IAsyncResult ar参数和void返回值. 最后一个参数可以传送任意对象.以便从回调方法中访问它.可以传送委托的实例.
TakesAWhileDelegate 完成了自己的任务,就会调用TakesAWhileCompleted 方法. 在回调方法TakesAWhileCompleted中,可以通过TakesAWhileDelegate d1 = ar.AsyncState as TakesAWhileDelegate; 获得BeginInvoke 传送的最后一个参数d1.
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()
{
// synchronous
// TakesAWhile(1, 3000);
TakesAWhileDelegate d1 = TakesAWhile;
// polling
//IAsyncResult ar = d1.BeginInvoke(1, 3000, null, null);
//while (!ar.IsCompleted)
//{
//// doing something else
// Console.Write(".");
// Thread.Sleep(50);
//}
//int result = d1.EndInvoke(ar);
//Console.WriteLine("result: {0}", result);
//Console.ReadLine();
//wait handle
//IAsyncResult ar = d1.BeginInvoke(1, 3000, null, null);
//while (true)
//{
// Console.Write(".");
// if (ar.AsyncWaitHandle.WaitOne(50, false))
// {
// Console.WriteLine("Can get the result now");
// break;
// }
//}
//int result = d1.EndInvoke(ar);
//Console.WriteLine("result: {0}", result);
//async callback
d1.BeginInvoke(1, 3000, TakesAWhileCompleted, d1);
for (int i = 0; i < 100; i++)
{
Console.Write(".");
Thread.Sleep(50);
}
//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);
}
}
}
在网络上查询了其他资料以后. 如http://developer.51cto.com/art/200908/141587.htm 中描述.用委托(Delegate)的BeginInvoke和EndInvoke方法操作线程:
在C#中使用线程的方法很多,使用委托的BeginInvoke和EndInvoke方法就是其中之一。BeginInvoke方法可以使用线程异步地执行委托所指向的方法。然后通过EndInvoke方法获得方法的返回值(EndInvoke方法的返回值就是被调用方法的返回值),或是确定方法已经被成功调用。我们可以通过四种方法从EndInvoke方法来获得返回值。1. 直接使用EndInvoke方法来获得返回值 2.使用IAsyncResult asyncResult属性来判断异步调用是否完成 3.使用WaitOne方法等待异步方法执行完成 4.使用回调方式返回结果
如果不调用EndInvoke方法,程序会立即退出,这是由于使用BeginInvoke创建的线程都是后台线程,这种线程一但所有的前台线程都退出后(其中主线程就是一个前台线程),不管后台线程是否执行完毕,都会结束线程,并退出程序。
使用回调方式返回结果 学习到此发现只有通过这种方式才能做一个多线程的进度条
上面介绍的几种方法实际上只相当于一种方法。这些方法虽然可以成功返回结果,也可以给用户一些提示,但在这个过程中,整个程序就象死了一样(如果读者在GUI程序中使用这些方法就会非常明显),要想在调用的过程中,程序仍然可以正常做其它的工作,就必须使用异步回调的方式。下面我们使用GUI程序来编写一个例子,代码如下:这个例子可以让界面不死
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private delegate int MyMethod();
private int method()
{
Thread.Sleep(10000); return 100;
}
private void MethodCompleted(IAsyncResult asyncResult)
{
if (asyncResult == null) return;
textBox1.Text = (asyncResult.AsyncState as MyMethod).EndInvoke(asyncResult).ToString();
}
private void button1_Click(object sender, EventArgs e)
{
MyMethod my = method;
IAsyncResult asyncResult = my.BeginInvoke(MethodCompleted, my);
}
}
}