简单易懂的C 简单易懂的财务报表表格模板
创始人
2024-05-07 06:41:20
0

 .NET将关于多线程的功能定义在System.Threading名字空间中。因此,要实现C#.NET多线程应用,必须先声明引用此名字空间(using System.Threading;)。

即使你没有编写c#.net多线程应用程序的经验,也可能听说过“启动线程”“杀死线程”这些词,其实除了这两个外,涉及多线程方面的还有诸如“暂停线程”“优先级”“挂起线程”“恢复线程”等等。下面将一个一个的解释。

a.启动线程

顾名思义,“启动线程”就是新建并启动一个线程的意思,如下代码可实现:   

  1. 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。具体实现例子如下:  

  1. thread.Priority = ThreadPriority.Highest; 

e.挂起线程

Thread类的Suspend方法用来挂起线程,知道调用Resume,此线程才可以继续执行。如果线程已经挂起,那就不会起作用。

  1. if (thread.ThreadState = ThreadState.Running)  
  2. {  
  3.      thread.Suspend();  

f.恢复线程

用来恢复已经挂起的线程,以让它继续执行,如果线程没挂起,也不会起作用。

  1. if (thread.ThreadState = ThreadState.Suspended)  
  2. {  
  3.      thread.Resume();  

下面将列出一个例子,以说明简单的线程处理功能。此例子来自于帮助文档。

  1. using System;  
  2. using System.Threading;  
  3.  
  4. // Simple threading scenario:  Start a static method running  
  5. // on a second thread.  
  6. public class ThreadExample {  
  7.     // The ThreadProc method is called when the thread starts.  
  8.     // It loops ten times, writing to the console and yielding  
  9.     // the rest of its time slice each time, and then ends.  
  10.     public static void ThreadProc() {  
  11.         for (int i = 0; i <  10; i++) {  
  12.             Console.WriteLine("ThreadProc: {0}", i);  
  13.             // Yield the rest of the time slice.  
  14.             Thread.Sleep(0);  
  15.         }  
  16.     }  
  17.  
  18.     public static void Main() {  
  19.         Console.WriteLine("Main thread: Start a second thread.");  
  20.         // The constructor for the Thread class requires a ThreadStart  
  21.         // delegate that represents the method to be executed on the  
  22.         // thread.  C# simplifies the creation of this delegate.  
  23.         Thread t = new Thread(new ThreadStart(ThreadProc));  
  24.         // Start ThreadProc.  On a uniprocessor, the thread does not get  
  25.         // any processor time until the main thread yields.  Uncomment  
  26.         // the Thread.Sleep that follows t.Start() to see the difference.  
  27.         t.Start();  
  28.         //Thread.Sleep(0);  
  29.  
  30.         for (int i = 0; i <  4; i++) {  
  31.             Console.WriteLine("Main thread: Do some work.");  
  32.             Thread.Sleep(0);  
  33.         }  
  34.  
  35.         Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.");  
  36.         t.Join();  
  37.         Console.WriteLine("Main thread: ThreadProc.Join has returned.  Press Enter to end program.");  
  38.         Console.ReadLine();  
  39.     }  

此代码产生的输出类似如下内容:

  1. Main thread: Start a second thread.  
  2. Main thread: Do some work.  
  3. ThreadProc: 0  
  4. Main thread: Do some work.  
  5. ThreadProc: 1  
  6. Main thread: Do some work.  
  7. ThreadProc: 2  
  8. Main thread: Do some work.  
  9. ThreadProc: 3  
  10. Main thread: Call Join(), to wait until ThreadProc ends.  
  11. ThreadProc: 4  
  12. ThreadProc: 5  
  13. ThreadProc: 6  
  14. ThreadProc: 7  
  15. ThreadProc: 8  
  16. ThreadProc: 9  
  17. Main thread: ThreadProc.Join has returned.  Press Enter to end program. 

C#.NET多线程应用的相关知识就介绍到这里,是不是非常简单呢?

【编辑推荐】

  1. 浅析C#启动停止SQL数据库服务之方法
  2. 总结C#获取当前路径的7种方法
  3. 浅析C# treeview控件的使用方法
  4. C#多态性的概念及其应用
  5. 介绍C#构造函数的使用方法

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
着眼MAC地址,解救无法享受D... 在安装了DHCP服务器的局域网环境中,每一台工作站在上网之前,都要先从DHCP服务器那里享受到地址动...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...