c#
c#综合揭秘——细说多线程(上) -凯发ag旗舰厅登录网址下载
引言
本文主要从线程的基础用法,clr线程池当中工作者线程与i/o线程的开发,并行操作plinq等多个方面介绍多线程的开发。
其中委托的begininvoke方法以及回调函数最为常用。
而 i/o线程可能容易遭到大家的忽略,其实在开发多线程系统,更应该多留意i/o线程的操作。特别是在asp.net开发当中,可能更多人只会留意在客户端使用ajax或者在服务器端使用updatepanel。其实合理使用i/o线程在通讯项目或文件下载时,能尽可能地减少iis的压力。
并行编程是framework4.0中极力推广的异步操作方式,更值得更深入地学习。
希望本篇文章能对各位的学习研究有所帮助,当中有所错漏的地方敬请点评。
目录
一、线程的定义
二、线程的基础知识
三、以threadstart方式实现多线程
四、clr线程池的工作者线程
五、clr线程池的i/o线程
六、异步 sqlcommand
七、并行编程与plinq
八、计时器与锁
一、线程的定义
1. 1 进程、应用程序域与线程的关系
进程(process)是windows系统中的一个基本概念,它包含着一个运行程序所需要的资源。进程之间是相对独立的,一个进程无法访问另一个进程的数据(除非利用分布式计算方式),一个进程运行的失败也不会影响其他进程的运行,windows系统就是利用进程把工作划分为多个独立的区域的。进程可以理解为一个程序的基本边界。
应用程序域(appdomain)是一个程序运行的逻辑区域,它可以视为一个轻量级的进程,.net的程序集正是在应用程序域中运行的,一个进程可以包含有多个应用程序域,一个应用程序域也可以包含多个程序集。在一个应用程序域中包含了一个或多个上下文context,使用上下文clr就能够把某些特殊对象的状态放置在不同容器当中。
线程(thread)是进程中的基本执行单元,在进程入口执行的第一个线程被视为这个进程的主线程。在.net应用程序中,都是以main()方法作为入口的,当调用此方法时系统就会自动创建一个主线程。线程主要是由cpu寄存器、调用栈和线程本地存储器(thread local storage,tls)组成的。cpu寄存器主要记录当前所执行线程的状态,调用栈主要用于维护线程所调用到的内存与数据,tls主要用于存放线程的状态信息。
进程、应用程序域、线程的关系如下图,一个进程内可以包括多个应用程序域,也有包括多个线程,线程也可以穿梭于多个应用程序域当中。但在同一个时刻,线程只会处于一个应用程序域内。
由于本文是以介绍多线程技术为主题,对进程、应用程序域的介绍就到此为止。关于进程、线程、应用程序域的技术,在“c#综合揭秘——细说进程、应用程序域与上下文”会有详细介绍。
1.2 多线程
在单cpu系统的一个单位时间(time slice)内,cpu只能运行单个线程,运行顺序取决于线程的优先级别。如果在单位时间内线程未能完成执行,系统就会把线程的状态信息保存到线程的本地存储器(tls) 中,以便下次执行时恢复执行。而多线程只是系统带来的一个假像,它在多个单位时间内进行多个线程的切换。因为切换频密而且单位时间非常短暂,所以多线程可被视作同时运行。
适当使用多线程能提高系统的性能,比如:在系统请求大容量的数据时使用多线程,把数据输出工作交给异步线程,使主线程保持其稳定性去处理其他问题。但需要注意一点,因为cpu需要花费不少的时间在线程的切换上,所以过多地使用多线程反而会导致性能的下降。
返回目录
二、线程的基础知识
2.1 system.threading.thread类
system.threading.thread是用于控制线程的基础类,通过thread可以控制当前应用程序域中线程的创建、挂起、停止、销毁。
它包括以下常用公共属性:
属性名称说明currentcontext | 获取线程正在其中执行的当前上下文。 |
currentthread | 获取当前正在运行的线程。 |
executioncontext | 获取一个 executioncontext 对象,该对象包含有关当前线程的各种上下文的信息。 |
isalive | 获取一个值,该值指示当前线程的执行状态。 |
isbackground | 获取或设置一个值,该值指示某个线程是否为后台线程。 |
isthreadpoolthread | 获取一个值,该值指示线程是否属于托管线程池。 |
managedthreadid | 获取当前托管线程的唯一标识符。 |
name | 获取或设置线程的名称。 |
priority | 获取或设置一个值,该值指示线程的调度优先级。 |
threadstate | 获取一个值,该值包含当前线程的状态。 |
2.1.1 线程的标识符
managedthreadid是确认线程的唯一标识符,程序在大部分情况下都是通过thread.managedthreadid来辨别线程的。而name是一个可变值,在默认时候,name为一个空值 null,开发人员可以通过程序设置线程的名称,但这只是一个辅助功能。
2.1.2 线程的优先级别
.net为线程设置了priority属性来定义线程执行的优先级别,里面包含5个选项,其中normal是默认值。除非系统有特殊要求,否则不应该随便设置线程的优先级别。
成员名称说明lowest | 可以将 thread 安排在具有任何其他优先级的线程之后。 |
belownormal | 可以将 thread 安排在具有 normal 优先级的线程之后,在具有 lowest 优先级的线程之前。 |
normal | 默认选择。可以将 thread 安排在具有 abovenormal 优先级的线程之后,在具有belownormal 优先级的线程之前。 |
abovenormal | 可以将 thread 安排在具有 highest 优先级的线程之后,在具有 normal 优先级的线程之前。 |
highest | 可以将 thread 安排在具有任何其他优先级的线程之前。 |
2.1.3 线程的状态
通过threadstate可以检测线程是处于unstarted、sleeping、running 等等状态,它比 isalive 属性能提供更多的特定信息。
前面说过,一个应用程序域中可能包括多个上下文,而通过currentcontext可以获取线程当前的上下文。
currentthread是最常用的一个属性,它是用于获取当前运行的线程。
2.1.4 system.threading.thread的方法
thread 中包括了多个方法来控制线程的创建、挂起、停止、销毁,以后来的例子中会经常使用。
方法名称说明abort() | 终止本线程。 |
getdomain() | 返回当前线程正在其中运行的当前域。 |
getdomainid() | 返回当前线程正在其中运行的当前域id。 |
interrupt() | 中断处于 waitsleepjoin 线程状态的线程。 |
join() | 已重载。 阻塞调用线程,直到某个线程终止时为止。 |
resume() | 继续运行已挂起的线程。 |
start() | 执行本线程。 |
suspend() | 挂起当前线程,如果当前线程已属于挂起状态则此不起作用 |
sleep() | 把正在运行的线程挂起一段时间。 |
2.1.5 开发实例
以下这个例子,就是通过thread显示当前线程信息
1 static void main(string[] args)2 {
3 thread thread = thread.currentthread;
4 thread.name = "main thread";
5 string threadmessage = string.format("thread id:{0}\n current appdomainid:{1}\n "
6 "current contextid:{2}\n thread name:{3}\n "
7 "thread state:{4}\n thread priority:{5}\n",
8 thread.managedthreadid, thread.getdomainid(), thread.currentcontext.contextid,
9 thread.name, thread.threadstate, thread.priority);
10 console.writeline(threadmessage);
11 console.readkey();
12 }
运行结果
2.2 system.threading 命名空间
在system.threading命名空间内提供多个方法来构建多线程应用程序,其中threadpool与thread是多线程开发中最常用到的,在.net中专门设定了一个clr线程池专门用于管理线程的运行,这个clr线程池正是通过threadpool类来管理。而thread是管理线程的最直接方式,下面几节将详细介绍有关内容。
类 | 说明 |
autoresetevent | 通知正在等待的线程已发生事件。无法继承此类。 |
executioncontext | 管理当前线程的执行上下文。无法继承此类。 |
interlocked | 为多个线程共享的变量提供原子操作。 |
monitor | 提供同步对对象的访问的机制。 |
mutex | 一个同步基元,也可用于进程间同步。 |
thread | 创建并控制线程,设置其优先级并获取其状态。 |
threadabortexception | 在对 abort 方法进行调用时引发的异常。无法继承此类。 |
threadpool | 提供一个线程池,该线程池可用于发送工作项、处理异步 i/o、代表其他线程等待以及处理计时器。 |
timeout | 包含用于指定无限长的时间的常数。无法继承此类。 |
timer | 提供以指定的时间间隔执行方法的机制。无法继承此类。 |
waithandle | 封装等待对共享资源的独占访问的操作系统特定的对象。 |
在system.threading中的包含了下表中的多个常用委托,其中threadstart、parameterizedthreadstart是最常用到的委托。
由threadstart生成的线程是最直接的方式,但由threadstart所生成并不受线程池管理。
而parameterizedthreadstart是为异步触发带参数的方法而设的,在下一节将为大家逐一细说。
contextcallback | 表示要在新上下文中调用的方法。 |
parameterizedthreadstart | 表示在 thread 上执行的方法。 |
threadexceptioneventhandler | 表示将要处理 application 的 threadexception 事件的方法。 |
threadstart | 表示在 thread 上执行的方法。 |
timercallback | 表示处理来自 timer 的调用的方法。 |
waitcallback | 表示线程池线程要执行的回调方法。 |
waitortimercallback | 表示当 waithandle 超时或终止时要调用的方法。 |
2.3 线程的管理方式
通过threadstart来创建一个新线程是最直接的方法,但这样创建出来的线程比较难管理,如果创建过多的线程反而会让系统的性能下载。有见及此,.net为线程管理专门设置了一个clr线程池,使用clr线程池系统可以更合理地管理线程的使用。所有请求的服务都能运行于线程池中,当运行结束时线程便会回归到线程池。通过设置,能控制线程池的最大线程数量,在请求超出线程最大值时,线程池能按照操作的优先级别来执行,让部分操作处于等待状态,待有线程回归时再执行操作。
基础知识就为大家介绍到这里,下面将详细介绍多线程的开发。
返回目录
三、以threadstart方式实现多线程
3.1 使用threadstart委托
这里先以一个例子体现一下多线程带来的好处,首先在message类中建立一个方法showmessage(),里面显示了当前运行线程的id,并使用thread.sleep(int ) 方法模拟部分工作。在main()中通过threadstart委托绑定message对象的showmessage()方法,然后通过thread.start()执行异步方法。
1 public class message2 {
3 public void showmessage()
4 {
5 string message = string.format("async threadid is :{0}",
6 thread.currentthread.managedthreadid);
7 console.writeline(message);
8
9 for (int n = 0; n < 10; n )
10 {
11 thread.sleep(300);
12 console.writeline("the number is:" n.tostring());
13 }
14 }
15 }
16
17 class program
18 {
19 static void main(string[] args)
20 {
21 console.writeline("main threadid is:"
22 thread.currentthread.managedthreadid);
23 message message=new message();
24 thread thread = new thread(new threadstart(message.showmessage));
25 thread.start();
26 console.writeline("do something ..........!");
27 console.writeline("main thread working is complete!");
28
29 }
30 }
请注意运行结果,在调用thread.start()方法后,系统以异步方式运行message.showmessage(),而主线程的操作是继续执行的,在message.showmessage()完成前,主线程已完成所有的操作。
3.2 使用parameterizedthreadstart委托
parameterizedthreadstart委托与threadstart委托非常相似,但parameterizedthreadstart委托是面向带参数方法的。注意parameterizedthreadstart 对应方法的参数为object,此参数可以为一个值对象,也可以为一个自定义对象。
1 public class person2 {
3 public string name
4 {
5 get;
6 set;
7 }
8 public int age
9 {
10 get;
11 set;
12 }
13 }
14
15 public class message
16 {
17 public void showmessage(object person)
18 {
19 if (person != null)
20 {
21 person _person = (person)person;
22 string message = string.format("\n{0}'s age is {1}!\nasync threadid is:{2}",
23 _person.name,_person.age,thread.currentthread.managedthreadid);
24 console.writeline(message);
25 }
26 for (int n = 0; n < 10; n )
27 {
28 thread.sleep(300);
29 console.writeline("the number is:" n.tostring());
30 }
31 }
32 }
33
34 class program
35 {
36 static void main(string[] args)
37 {
38 console.writeline("main threadid is:" thread.currentthread.managedthreadid);
39
40 message message=new message();
41 //绑定带参数的异步方法
42 thread thread = new thread(new parameterizedthreadstart(message.showmessage));
43 person person = new person();
44 person.name = "jack";
45 person.age = 21;
46 thread.start(person); //启动异步线程
47
48 console.writeline("do something ..........!");
49 console.writeline("main thread working is complete!");
50
51 }
52 }
运行结果:
3.3 前台线程与后台线程
注意以上两个例子都没有使用console.readkey(),但系统依然会等待异步线程完成后才会结束。这是因为使用thread.start()启动的线程默认为前台线程,而系统必须等待所有前台线程运行结束后,应用程序域才会自动卸载。
在第二节曾经介绍过线程thread有一个属性isbackground,通过把此属性设置为true,就可以把线程设置为后台线程!这时应用程序域将在主线程完成时就被卸载,而不会等待异步线程的运行。
3.4 挂起线程
为了等待其他后台线程完成后再结束主线程,就可以使用thread.sleep()方法。
1 public class message2 {
3 public void showmessage()
4 {
5 string message = string.format("\nasync threadid is:{0}",
6 thread.currentthread.managedthreadid);
7 console.writeline(message);
8 for (int n = 0; n < 10; n )
9 {
10 thread.sleep(300);
11 console.writeline("the number is:" n.tostring());
12 }
13 }
14 }
15
16 class program
17 {
18 static void main(string[] args)
19 {
20 console.writeline("main threadid is:"
21 thread.currentthread.managedthreadid);
22
23 message message=new message();
24 thread thread = new thread(new threadstart(message.showmessage));
25 thread.isbackground = true;
26 thread.start();
27
28 console.writeline("do something ..........!");
29 console.writeline("main thread working is complete!");
30 console.writeline("main thread sleep!");
31 thread.sleep(5000);
32 }
33 }
运行结果如下,此时应用程序域将在主线程运行5秒后自动结束
但系统无法预知异步线程需要运行的时间,所以用通过thread.sleep(int)阻塞主线程并不是一个好的解决方法。有见及此,.net专门为等待异步线程完成开发了另一个方法thread.join()。把上面例子中的最后一行thread.sleep(5000)修改为 thread.join() 就能保证主线程在异步线程thread运行结束后才会终止。
3.5 suspend 与 resume (慎用)
thread.suspend()与 thread.resume()是在framework1.0 就已经存在的老方法了,它们分别可以挂起、恢复线程。但在framework2.0中就已经明确排斥这两个方法。这是因为一旦某个线程占用了已有的资源,再使用suspend()使线程长期处于挂起状态,当在其他线程调用这些资源的时候就会引起死锁!所以在没有必要的情况下应该避免使用这两个方法。
3.6 终止线程
若想终止正在运行的线程,可以使用abort()方法。在使用abort()的时候,将引发一个特殊异常 threadabortexception 。
若想在线程终止前恢复线程的执行,可以在捕获异常后 ,在catch(threadabortexception ex){...} 中调用thread.resetabort()取消终止。
而使用thread.join()可以保证应用程序域等待异步线程结束后才终止运行。
2 {
3 console.writeline("main threadid is:"
4 thread.currentthread.managedthreadid);
5
6 thread thread = new thread(new threadstart(asyncthread));
7 thread.isbackground = true;
8 thread.start();
9 thread.join();
10
11 }
12
13 //以异步方式调用
14 static void asyncthread()
15 {
16 try
17 {
18 string message = string.format("\nasync threadid is:{0}",
19 thread.currentthread.managedthreadid);
20 console.writeline(message);
21
22 for (int n = 0; n < 10; n )
23 {
24 //当n等于4时,终止线程
25 if (n >= 4)
26 {
27 thread.currentthread.abort(n);
28 }
29 thread.sleep(300);
30 console.writeline("the number is:" n.tostring());
31 }
32 }
33 catch (threadabortexception ex)
34 {
35 //输出终止线程时n的值
36 if (ex.exceptionstate != null)
37 console.writeline(string.format("thread abort when the number is: {0}!",
38 ex.exceptionstate.tostring()));
39
40 //取消终止,继续执行线程
41 thread.resetabort();
42 console.writeline("thread resetabort!");
43 }
44
45 //线程结束
46 console.writeline("thread close!");
47 }
运行结果如下
返回目录
四、clr线程池的工作者线程
4.1 关于clr线程池
使用threadstart与parameterizedthreadstart建立新线程非常简单,但通过此方法建立的线程难于管理,若建立过多的线程反而会影响系统的性能。
有见及此,.net引入clr线程池这个概念。clr线程池并不会在clr初始化的时候立刻建立线程,而是在应用程序要创建线程来执行任务时,线程池才初始化一个线程。线程的初始化与其他的线程一样。在完成任务以后,该线程不会自行销毁,而是以挂起的状态返回到线程池。直到应用程序再次向线程池发出请求时,线程池里挂起的线程就会再度激活执行任务。这样既节省了建立线程所造成的性能损耗,也可以让多个任务反复重用同一线程,从而在应用程序生存期内节约大量开销。
注意:通过clr线程池所建立的线程总是默认为后台线程,优先级数为threadpriority.normal。
4.2 工作者线程与i/o线程
clr线程池分为工作者线程(workerthreads)与i/o线程 (completionportthreads) 两种,工作者线程是主要用作管理clr内部对象的运作,i/o(input/output) 线程顾名思义是用于与外部系统交换信息,io线程的细节将在下一节详细说明。
通过threadpool.getmax(out int workerthreads,out int completionportthreads )和 threadpool.setmax( int workerthreads, int completionportthreads)两个方法可以分别读取和设置clr线程池中工作者线程与i/o线程的最大线程数。在framework2.0中最大线程默认为25*cpu数,在framewok3.0、4.0中最大线程数默认为250*cpu数,在近年 i3,i5,i7 cpu出现后,线程池的最大值一般默认为1000、2000。
若想测试线程池中有多少的线程正在投入使用,可以通过threadpool.getavailablethreads( out int workerthreads,out int completionportthreads ) 方法。
使用clr线程池的工作者线程一般有两种方式,一是直接通过 threadpool.queueuserworkitem() 方法,二是通过委托,下面将逐一细说。
4.3 通过queueuserworkitem启动工作者线程
threadpool线程池中包含有两个静态方法可以直接启动工作者线程:
一为 threadpool.queueuserworkitem(waitcallback)
二为 threadpool.queueuserworkitem(waitcallback,object)
先把waitcallback委托指向一个带有object参数的无返回值方法,再使用 threadpool.queueuserworkitem(waitcallback) 就可以异步启动此方法,此时异步方法的参数被视为null 。
1 class program2 {
3 static void main(string[] args)
4 {
5 //把clr线程池的最大值设置为1000
6 threadpool.setmaxthreads(1000, 1000);
7 //显示主线程启动时线程池信息
8 threadmessage("start");
9 //启动工作者线程
10 threadpool.queueuserworkitem(new waitcallback(asynccallback));
11 console.readkey();
12 }
13
14 static void asynccallback(object state)
15 {
16 thread.sleep(200);
17 threadmessage("asynccallback");
18 console.writeline("async thread do work!");
19 }
20
21 //显示线程现状
22 static void threadmessage(string data)
23 {
24 string message = string.format("{0}\n currentthreadid is {1}",
25 data, thread.currentthread.managedthreadid);
26 console.writeline(message);
27 }
28 }
运行结果
使用 threadpool.queueuserworkitem(waitcallback,object) 方法可以把object对象作为参数传送到回调函数中。
下面例子中就是把一个string对象作为参数发送到回调函数当中。
2 {
3 static void main(string[] args)
4 {
5 //把线程池的最大值设置为1000
6 threadpool.setmaxthreads(1000, 1000);
7
8 threadmessage("start");
9 threadpool.queueuserworkitem(new waitcallback(asynccallback),"hello elva");
10 console.readkey();
11 }
12
13 static void asynccallback(object state)
14 {
15 thread.sleep(200);
16 threadmessage("asynccallback");
17
18 string data = (string)state;
19 console.writeline("async thread do work!\n" data);
20 }
21
22 //显示线程现状
23 static void threadmessage(string data)
24 {
25 string message = string.format("{0}\n currentthreadid is {1}",
26 data, thread.currentthread.managedthreadid);
27 console.writeline(message);
28 }
29 }
运行结果
通过threadpool.queueuserworkitem启动工作者线程虽然是方便,但waitcallback委托指向的必须是一个带有object参数的无返回值方法,这无疑是一种限制。若方法需要有返回值,或者带有多个参数,这将多费周折。有见及此,.net提供了另一种方式去建立工作者线程,那就是委托。
4.4 委托类
使用clr线程池中的工作者线程,最灵活最常用的方式就是使用委托的异步方法,在此先简单介绍一下委托类。
当定义委托后,.net就会自动创建一个代表该委托的类,下面可以用反射方式显示委托类的方法成员(对反射有兴趣的朋友可以先参考一下“.net基础篇——反射的奥妙”)
1 class program2 {
3 delegate void mydelegate();
4
5 static void main(string[] args)
6 {
7 mydelegate delegate1 = new mydelegate(asyncthread);
8 //显示委托类的几个方法成员
9 var methods=delegate1.gettype().getmethods();
10 if (methods != null)
11 foreach (methodinfo info in methods)
12 console.writeline(info.name);
13 console.readkey();
14 }
15 }
委托类包括以下几个重要方法
1 public class mydelegate:multicastdelegate2 {
3 public mydelegate(object target, int methodptr);
4 //调用委托方法
5 public virtual void invoke();
6 //异步委托
7 public virtual iasyncresult begininvoke(asynccallback callback,object state);
8 public virtual void endinvoke(iasyncresult result);
9 }
当调用invoke()方法时,对应此委托的所有方法都会被执行。而begininvoke与endinvoke则支持委托方法的异步调用,由begininvoke启动的线程都属于clr线程池中的工作者线程,在下面将详细说明。
4.5 利用begininvoke与endinvoke完成异步委托方法
首先建立一个委托对象,通过iasyncresult begininvoke(string name,asynccallback callback,object state) 异步调用委托方法,begininvoke 方法除最后的两个参数外,其它参数都是与方法参数相对应的。通过 begininvoke 方法将返回一个实现了 system.iasyncresult 接口的对象,之后就可以利用endinvoke(iasyncresult ) 方法就可以结束异步操作,获取委托的运行结果。
1 class program2 {
3 delegate string mydelegate(string name);
4
5 static void main(string[] args)
6 {
7 threadmessage("main thread");
8
9 //建立委托
10 mydelegate mydelegate = new mydelegate(hello);
11 //异步调用委托,获取计算结果
12 iasyncresult result=mydelegate.begininvoke("leslie", null, null);
13 //完成主线程其他工作
14 .............
15 //等待异步方法完成,调用endinvoke(iasyncresult)获取运行结果
16 string data=mydelegate.endinvoke(result);
17 console.writeline(data);
18
19 console.readkey();
20 }
21
22 static string hello(string name)
23 {
24 threadmessage("async thread");
25 thread.sleep(2000); //虚拟异步工作
26 return "hello " name;
27 }
28
29 //显示当前线程
30 static void threadmessage(string data)
31 {
32 string message = string.format("{0}\n threadid is:{1}",
33 data,thread.currentthread.managedthreadid);
34 console.writeline(message);
35 }
36 }
运行结果
4.6 善用iasyncresult
在以上例子中可以看见,如果在使用mydelegate.begininvoke后立即调用mydelegate.endinvoke,那在异步线程未完成工作以前主线程将处于阻塞状态,等到异步线程结束获取计算结果后,主线程才能继续工作,这明显无法展示出多线程的优势。此时可以好好利用iasyncresult 提高主线程的工作性能,iasyncresult有以下成员:
1 public interface iasyncresult2 {
3 object asyncstate {get;} //获取用户定义的对象,它限定或包含关于异步操作的信息。
4 wailhandle asyncwaithandle {get;} //获取用于等待异步操作完成的 waithandle。
5 bool completedsynchronously {get;} //获取异步操作是否同步完成的指示。
6 bool iscompleted {get;} //获取异步操作是否已完成的指示。
7 }
通过轮询方式,使用iscompleted属性判断异步操作是否完成,这样在异步操作未完成前就可以让主线程执行另外的工作。
1 class program2 {
3 delegate string mydelegate(string name);
4
5 static void main(string[] args)
6 {
7 threadmessage("main thread");
8
9 //建立委托
10 mydelegate mydelegate = new mydelegate(hello);
11 //异步调用委托,获取计算结果
12 iasyncresult result=mydelegate.begininvoke("leslie", null, null);
13 //在异步线程未完成前执行其他工作
14 while (!result.iscompleted)
15 {
16 thread.sleep(200); //虚拟操作
17 console.writeline("main thead do work!");
18 }
19 string data=mydelegate.endinvoke(result);
20 console.writeline(data);
21
22 console.readkey();
23 }
24
25 static string hello(string name)
26 {
27 threadmessage("async thread");
28 thread.sleep(2000);
29 return "hello " name;
30 }
31
32 static void threadmessage(string data)
33 {
34 string message = string.format("{0}\n threadid is:{1}",
35 data,thread.currentthread.managedthreadid);
36 console.writeline(message);
37 }
38 }
运行结果:
除此以外,也可以使用wailhandle完成同样的工作,waithandle里面包含有一个方法waitone(int timeout),它可以判断委托是否完成工作,在工作未完成前主线程可以继续其他工作。运行下面代码可得到与使用 iasyncresult.iscompleted 同样的结果,而且更简单方便 。
1 namespace test2 {
3 class program
4 {
5 delegate string mydelegate(string name);
6
7 static void main(string[] args)
8 {
9 threadmessage("main thread");
10
11 //建立委托
12 mydelegate mydelegate = new mydelegate(hello);
13
14 //异步调用委托,获取计算结果
15 iasyncresult result=mydelegate.begininvoke("leslie", null, null);
16
17 while (!result.asyncwaithandle.waitone(200))
18 {
19 console.writeline("main thead do work!");
20 }
21 string data=mydelegate.endinvoke(result);
22 console.writeline(data);
23
24 console.readkey();
25 }
26
27 static string hello(string name)
28 {
29 threadmessage("async thread");
30 thread.sleep(2000);
31 return "hello " name;
32 }
33
34 static void threadmessage(string data)
35 {
36 string message = string.format("{0}\n threadid is:{1}",
37 data,thread.currentthread.managedthreadid);
38 console.writeline(message);
39 }
40 }
当要监视多个运行对象的时候,使用iasyncresult.waithandle.waitone可就派不上用场了。
幸好.net为waithandle准备了另外两个静态方法:waitany(waithandle[], int)与waitall (waithandle[] , int)。
其中waitall在等待所有waithandle完成后再返回一个bool值。
而waitany是等待其中一个waithandle完成后就返回一个int,这个int是代表已完成waithandle在waithandle[]中的数组索引。
下面就是使用waitall的例子,运行结果与使用 iasyncresult.iscompleted 相同。
2 {
3 delegate string mydelegate(string name);
4
5 static void main(string[] args)
6 {
7 threadmessage("main thread");
8
9 //建立委托
10 mydelegate mydelegate = new mydelegate(hello);
11
12 //异步调用委托,获取计算结果
13 iasyncresult result=mydelegate.begininvoke("leslie", null, null);
14
15 //此处可加入多个检测对象
16 waithandle[] waithandlelist = new waithandle[] { result.asyncwaithandle,........ };
17 while (!waithandle.waitall(waithandlelist,200))
18 {
19 console.writeline("main thead do work!");
20 }
21 string data=mydelegate.endinvoke(result);
22 console.writeline(data);
23
24 console.readkey();
25 }
26
27 static string hello(string name)
28 {
29 threadmessage("async thread");
30 thread.sleep(2000);
31 return "hello " name;
32 }
33
34 static void threadmessage(string data)
35 {
36 string message = string.format("{0}\n threadid is:{1}",
37 data,thread.currentthread.managedthreadid);
38 console.writeline(message);
39 }
40 }
4.7 回调函数
使用轮询方式来检测异步方法的状态非常麻烦,而且效率不高,有见及此,.net为 iasyncresult begininvoke(asynccallback , object)准备了一个回调函数。使用 asynccallback 就可以绑定一个方法作为回调函数,回调函数必须是带参数 iasyncresult 且无返回值的方法: void asycncallbackmethod(iasyncresult result) 。在begininvoke方法完成后,系统就会调用asynccallback所绑定的回调函数,最后回调函数中调用 xxx endinvoke(iasyncresult result) 就可以结束异步方法,它的返回值类型与委托的返回值一致。
1 class program2 {
3 delegate string mydelegate(string name);
4
5 static void main(string[] args)
6 {
7 threadmessage("main thread");
8
9 //建立委托
10 mydelegate mydelegate = new mydelegate(hello);
11 //异步调用委托,获取计算结果
12 mydelegate.begininvoke("leslie", new asynccallback(completed), null);
13 //在启动异步线程后,主线程可以继续工作而不需要等待
14 for (int n = 0; n < 6; n )
15 console.writeline(" main thread do work!");
16 console.writeline("");
17
18 console.readkey();
19 }
20
21 static string hello(string name)
22 {
23 threadmessage("async thread");
24 thread.sleep(2000); \\模拟异步操作
25 return "\nhello " name;
26 }
27
28 static void completed(iasyncresult result)
29 {
30 threadmessage("async completed");
31
32 //获取委托对象,调用endinvoke方法获取运行结果
33 asyncresult _result = (asyncresult)result;
34 mydelegate mydelegate = (mydelegate)_result.asyncdelegate;
35 string data = mydelegate.endinvoke(_result);
36 console.writeline(data);
37 }
38
39 static void threadmessage(string data)
40 {
41 string message = string.format("{0}\n threadid is:{1}",
42 data, thread.currentthread.managedthreadid);
43 console.writeline(message);
44 }
45 }
可以看到,主线在调用begininvoke方法可以继续执行其他命令,而无需再等待了,这无疑比使用轮询方式判断异步方法是否完成更有优势。
在异步方法执行完成后将会调用asynccallback所绑定的回调函数,注意一点,回调函数依然是在异步线程中执行,这样就不会影响主线程的运行,这也使用回调函数最值得青昧的地方。
在回调函数中有一个既定的参数iasyncresult,把iasyncresult强制转换为asyncresult后,就可以通过 asyncresult.asyncdelegate 获取原委托,再使用endinvoke方法获取计算结果。
运行结果如下:
如果想为回调函数传送一些外部信息,就可以利用begininvoke(asynccallback,object)的最后一个参数object,它允许外部向回调函数输入任何类型的参数。只需要在回调函数中利用 asyncresult.asyncstate 就可以获取object对象。
2 {
3 public class person
4 {
5 public string name;
6 public int age;
7 }
8
9 delegate string mydelegate(string name);
10
11 static void main(string[] args)
12 {
13 threadmessage("main thread");
14
15 //建立委托
16 mydelegate mydelegate = new mydelegate(hello);
17
18 //建立person对象
19 person person = new person();
20 person.name = "elva";
21 person.age = 27;
22
23 //异步调用委托,输入参数对象person, 获取计算结果
24 mydelegate.begininvoke("leslie", new asynccallback(completed), person);
25
26 //在启动异步线程后,主线程可以继续工作而不需要等待
27 for (int n = 0; n < 6; n )
28 console.writeline(" main thread do work!");
29 console.writeline("");
30
31 console.readkey();
32 }
33
34 static string hello(string name)
35 {
36 threadmessage("async thread");
37 thread.sleep(2000);
38 return "\nhello " name;
39 }
40
41 static void completed(iasyncresult result)
42 {
43 threadmessage("async completed");
44
45 //获取委托对象,调用endinvoke方法获取运行结果
46 asyncresult _result = (asyncresult)result;
47 mydelegate mydelegate = (mydelegate)_result.asyncdelegate;
48 string data = mydelegate.endinvoke(_result);
49 //获取person对象
50 person person = (person)result.asyncstate;
51 string message = person.name "'s age is " person.age.tostring();
52
53 console.writeline(data "\n" message);
54 }
55
56 static void threadmessage(string data)
57 {
58 string message = string.format("{0}\n threadid is:{1}",
59 data, thread.currentthread.managedthreadid);
60 console.writeline(message);
61 }
62 }
运行结果:
转载于:https://www.cnblogs.com/ywsoftware/p/3147668.html
总结
以上是凯发ag旗舰厅登录网址下载为你收集整理的c#综合揭秘——细说多线程(上)的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得凯发ag旗舰厅登录网址下载网站内容还不错,欢迎将凯发ag旗舰厅登录网址下载推荐给好友。
- 上一篇:
- 下一篇: