C#线程类的定义实例解析

C#线程类的定义实例实现是如何的呢?我们使用Thread类,将Thread类封装在一个MyThread类中,以使任何从MyThread继承的类都具有多线程能力。MyThread类的代码如下:

C#线程类的定义实例:

 
 
 
  1. //C#线程类的定义实例  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.Threading;  
  7. namespace MyThread  
  8. {  
  9.    abstract class MyThread  
  10.     {  
  11.        Thread thread = null;  
  12.  
  13.        abstract public void run();  
  14.  
  15.         public void start()  
  16.         {  
  17.             if (thread == null)  
  18.                 thread = new Thread(run);  
  19.             thread.Start();  
  20.         }  
  21.     }  
  22. }  

C#线程类的定义之使用MyThread类:

 
 
 
  1. class NewThread : MyThread  
  2. {  
  3.       override public void run()  
  4.       {  
  5.       Console.WriteLine("使用MyThread建立并运行线程");  
  6.       }  
  7.   }  
  8.  
  9.   static void Main(string[] args)  
  10.   {  
  11.  
  12.       NewThread nt = new NewThread();  
  13.       nt.start();  
  14.   }  

C#线程类的定义实例基本的内容就向你介绍到这里,希望对你了解和学习C#线程类的定义有所帮助。

【编辑推荐】

  1. C#静态类和静态类成员详解
  2. C# byte数组常用扩展浅析
  3. 浅析C#byte数组转化成图像的实现
  4. C#线程概述及视图解析
  5. C#线程操作常见的六大操作方法
THE END