您的位置:首页 > 其它

关于异步事件的一个测试及其分析结果

2004-12-20 09:13 603 查看
因为工作中需要在多线程中使用事件,本来想在事件中抛出一个异常到主程序中,结果可想而知,失败了。于是作了如下的一个测试,看看多线程中的异常处理问题,代码如下:

using System;

using System.Runtime.InteropServices;

using System.Threading;

namespace testAsyncEvent

代码的执行结果如下:

Begin

Thread Started

root: Cycle Print

child(er0):     Enter Thread:child

(er0):  Enter Thread:child

root: Cycle Print

child(er0):     0

(er0):  0

(er0):  1

child(er0):     1

root: Cycle Print

(er0):  2

child(er0):     2

(er0):  3

child(er0):     3

(er0):  4

child(er0):     4

(er0):  5

child(er0):     5

(er0):  6

child(er0):     6

root: Cycle Print

(er0):  7

child(er0):     7

(er0):  8

child(er0):     8

(er0):  Exit Thread:child

child(er0):     Exit Thread:child

root: Cycle Print

root: Cycle Print

root: Cycle Print

root: Cycle Print

root: Cycle Print

root: Cycle Print

Ok

Press Enter to Exit ...

从结果分析,发现如果在线程中发生同步事件,则事件的发生源在同一个线程中,如果线程中发生异步事件,则该事件既不在主线程中,也不再子线程中,而是在系统的临时线程中。

但是这里有一个问题,我无法区分线程的真正不同,哪里可以取得线程号阿?谁能告诉我,这样有线程号的区别,这个例子可以更清楚一些。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: