简单易懂的C#.NET多线程应用

开发 后端
本文从启动线程、杀死线程、暂停线程、优先级、挂起线程、恢复线程等方面介绍了C#.NET多线程应用。希望对大家有所帮助。

 .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#构造函数的使用方法
责任编辑:book05 来源: 新浪博客
相关推荐

2009-08-24 16:30:43

C#.NET绑定Off

2009-09-11 11:30:53

Net60C#.NET

2011-06-17 15:55:19

ArrayListC#

2009-08-26 14:23:14

C#.Net Fram

2009-08-25 13:53:20

C#.NET rege

2009-08-26 10:09:22

C#编码规范

2009-08-24 16:19:54

C#.NET绑定Off

2009-08-13 10:35:55

C#.NET操作XML

2009-08-19 15:44:09

ObjectARX .

2009-10-09 17:01:32

VB.NET多线程

2009-09-01 17:15:42

C#多线程应用

2009-10-20 10:23:08

VB.NET多线程编程

2009-04-02 15:21:43

c#IDisposeFinalize

2009-09-01 16:14:05

ArrayList与A

2009-10-27 12:20:06

VB.NET多线程应用

2009-08-19 16:19:33

Employee对象

2010-03-24 10:32:05

Python多线程

2021-05-27 08:47:16

C语言C语言程序开发

2009-08-28 09:29:02

2009-08-19 16:05:46

AutoCADEditor类
点赞
收藏

51CTO技术栈公众号