上一篇:Visual Studio2005改变软件教育模式 >>
Visual C#中编写多线程程序之起步
即使你没有编写多线程应用程序的经验,也可能听说过“启动线程”“杀死线程”这些词,其实除了这两个外,涉及多线程方面的还有诸如“暂停线程”“优先级”“挂起线程”“恢复线程”等等。下面将一个一个的解释。
a.启动线程
顾名思义,“启动线程”就是新建并启动一个线程的意思,如下代码可实现:
| Thread thread1 = new Thread(new ThreadStart( Count)); |
其中的 Count 是将要被新线程执行的函数。
b.杀死线程
“杀死线程”就是将一线程斩草除根,为了不白费力气,在杀死一个线程前最好先判断它是否还活着(通过 IsAlive 属性),然后就可以调用 Abort 方法来杀死此线程。
c.暂停线程
它的意思就是让一个正在运行的线程休眠一段时间。如 thread.Sleep(1000); 就是让线程休眠1秒钟。
d.优先级
这个用不着解释了。Thread类中有一个ThreadPriority属性,它用来设置优先级,但不能保证操作系统会接受该优先级。一个线程的优先级可 分为5种:Normal, AboveNormal, BelowNormal, Highest, Lowest。具体实现例子如下:
| thread.Priority = ThreadPriority.Highest; |
e.挂起线程
Thread类的Suspend方法用来挂起线程,知道调用Resume,此线程才可以继续执行。如果线程已经挂起,那就不会起作用。
| if (thread.ThreadState = ThreadState.Running) { thread.Suspend(); } |
f.恢复线程
用来恢复已经挂起的线程,以让它继续执行,如果线程没挂起,也不会起作用。
| if (thread.ThreadState = ThreadState.Suspended) { thread.Resume(); } |
下面将列出一个例子,以说明简单的线程处理功能。此例子来自于帮助文档。
| using System; using System.Threading; // Simple threading scenario: Start a static method running // on a second thread. public class ThreadExample { // The ThreadProc method is called when the thread starts. // It loops ten times, writing to the console and yielding // the rest of its time slice each time, and then ends. public static void ThreadProc() { for (int i = 0; i < 10; i++) { Console.WriteLine("ThreadProc: {0}", i); // Yield the rest of the time slice. Thread.Sleep(0); } } public static void Main() { Console.WriteLine("Main thread: Start a second thread."); // The constructor for the Thread class requires a ThreadStart // delegate that represents the method to be executed on the // thread. C# simplifies the creation of this delegate. Thread t = new Thread(new ThreadStart(ThreadProc)); // Start ThreadProc. On a uniprocessor, the thread does not get // any processor time until the main thread yields. Uncomment // the Thread.Sleep that follows t.Start() to see the difference. t.Start(); //Thread.Sleep(0); for (int i = 0; i < 4; i++) { Console.WriteLine("Main thread: Do some work."); Thread.Sleep(0); } Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends."); t.Join(); Console.WriteLine("Main thread: ThreadProc.Join has returned. Press Enter to end program."); Console.ReadLine(); } } |
此代码产生的输出类似如下内容:
| Main thread: Start a second thread. Main thread: Do some work. ThreadProc: 0 Main thread: Do some work. ThreadProc: 1 Main thread: Do some work. ThreadProc: 2 Main thread: Do some work. ThreadProc: 3 Main thread: Call Join(), to wait until ThreadProc ends. ThreadProc: 4 ThreadProc: 5 ThreadProc: 6 ThreadProc: 7 ThreadProc: 8 ThreadProc: 9 Main thread: ThreadProc.Join has returned. Press Enter to end program. |
下一篇:Visual C#实现文件分割合并器 >>
相关文章:
- · 更改IE下载文件后的默认处理方法
- · 网管必备的智能监控软件:网络执法官
- · 用Delphi设计循环播放声音文件程序
- · Delphi编程禁止用户关闭操作系统
- · PHP 5.0 中的对象重载技术研究
- · 美政府黑客大会寻人才 培养黑客界线人
- · 腾讯RTX能带给企业客户的价值
- · 腾讯RTX提供给用户的主要解决方案
- · 腾讯RTX的主要功能特性介绍
- · MM购物人人埋单 人人网重金搜寻品味美女
- · 是猫友就要有猫窝!猫扑我的空间试用小记
- · UUME引领网络视频娱乐狂潮
- · 让我们停止这场战争
- · 一个“小圈子”,聚拢大家庭
- · 个人博客盈利并不难 要根据特点选对广告
- · 猫扑网友热心公益 齐力捐助善良老人
- · 猫扑网友自拍漫画玩火世界杯
- · 美少女足球队让你High到爽
- · 激情盛夏 趋势科技送大礼
- · 把软件当硬件卖 趋势给中国市场的独创方案
- · 我的媒体中心—“猫仔队1.7”新鲜登场
- · 猫扑音乐随身听,保护数字音乐版权有新招
- · 网络创业者们看过来 网站的盈利方式
- · 警报:转信服务已危及全球Internet安全
- · 百度硬盘搜索2.0 Beta版抢先试用
- · 继续前行 百度硬盘搜索发布一周年
- · 百度超级搜霸有六大搜索利器
- · 百度搜霸——让你做个精明的管家
- · 启动3D聊天模式 与Q友站到系统桌面上畅聊
- · 体验腾讯QQ新酷功能——3D秀
- · QQ新版3月发布 增3D秀、网络城市等新元素
- · QQ宠物常见问题解答(十四)
- · QQ宠物常见问题解答(十五)
- · 快速学用腾讯TM:方便的个人名片
- · 快速学用腾讯TM:自定义个人形象
- · 快速学用腾讯TM:TM通讯录
- · 快速学用腾讯TM:商务伴侣
- · QQ宠物常见问题解答(十七)
