一种基于Thread的线程简化使用方法
一、CThreadBase类,实现对线程开发基本包装,包括线程参数传递
/// <summary> /// 线程封装类: /// </summary> public class CThreadBase { public string[] sArgs; //0必须是线程名称, public int[] iArgs; //整形的参数 public object obj; //特定类型参数 public CThreadBase(string[] sArgs, int[] iArgs, object obj) { this.sArgs = sArgs; ; this.iArgs = iArgs; this.obj = obj; } public CThreadBase(string[] sArgs, int[] iArgs) { this.sArgs = sArgs; this.iArgs = iArgs; this.obj = null; } public CThreadBase(string[] sArgs) { this.sArgs = sArgs; ; this.iArgs = null; this.obj = null; } public CThreadBase(int[] iArgs) { this.sArgs = null; ; this.iArgs = iArgs; this.obj = null; } public CThreadBase(object obj) { this.sArgs = null; ; this.iArgs = null; this.obj = obj; } public CThreadBase() { } public virtual void ThreadFunction() { for (int i = 0; i < iArgs[0]; i++) { Console.WriteLine("I am {0},I am working,Be easy! {1}", sArgs[0], DateTime.Now.ToLongTimeString()); Thread.Sleep(iArgs[1]); } } public Thread Run() { Thread t = new Thread(new ThreadStart(ThreadFunction)); t.Name = sArgs[0]; t.Start(); return t; } }
二、CThreadManage,线程管理类,主要用于与线程通讯及监视
/// <summary> /// 线程管理类 /// </summary> public class CThreadManage { public DateTime d; public List<string> ThreadName; public int ThreadCount; public List<Thread> ThreadList; public CThreadManage() { // ThreadName = new List<string>(); ThreadList = new List<Thread>(); ThreadCount = 0; } public void Add(Thread tt) { ThreadName.Add(tt.Name); ThreadList.Add(tt); ThreadCount++; } public void KillThread() { int count = ThreadCount; for (int i = 0; i < count; i++) { Console.WriteLine("Killing {0}, {1}", ThreadName[0], ThreadCount); KillThread(0); //Console.WriteLine("Killing {0}, {1}", ThreadName[0], ThreadCount); } } public void KillThread(string name) { for (int i = 0; i < ThreadCount; i++) { if (ThreadName[i] == name) { KillThread(i); } } } public void KillThread(int i) { if (i < ThreadCount) { ThreadList[i].Abort(); ThreadCount--; ThreadList.RemoveAt(i); ThreadName.RemoveAt(i); } } ~CThreadManage() { int count = ThreadCount; for (int i = 0; i < count; i++) { KillThread(0); } } }
三、应用类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class CMyThreadExample : CThreadBase { public CMyThreadExample(string[] sargs, int[] iargs) : base(sargs, iargs) { } public override void ThreadFunction() { for (int i = 0; i < iArgs[0]; i++) { Console.WriteLine("I am {0} from sublcass ,I am working,Be easy! {1}", sArgs[0], DateTime.Now.ToLongTimeString()); Thread.Sleep(iArgs[1]); } } }
四、一个例子
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
namespace TestThread { class Program { static void Main(string[] args) { ThreadManage rp = new ThreadManage(); string[] sargs = { "Thread 001" }; int[] iargs = { 10000, 2000 }; CThreadBase threadArgs = new CThreadBase(sargs, iargs); rp.Add(threadArgs.Run()); string[] sargs1 = { "Thread 002" }; rp.Add(threadArgs1.Run()); string[] sargs2 = { "Thread 003" }; CMyThreadExample mythrad = new CMyThreadExample(sargs2, iargs); rp.Add(mythrad.Run()); Console.WriteLine(rp.ThreadCount.ToString()); while (true) { ConsoleKey InputKey = Console.ReadKey().Key; Console.WriteLine(InputKey.ToString()); if (InputKey == ConsoleKey.Escape) { rp.KillThread(); break; } else { if (InputKey == ConsoleKey.D1) { rp.KillThread(0); } } } Console.WriteLine("i am ending"); } } }