C#中 一个多线程框架

C#提供了丰富的多线程操作,为编程带来了极大的便利,但如果使用不当也会带来各种各样的麻烦。

这里把C#中多线的操作进行了一下简单的封装,使它对外提供的接口简洁,易于控制。

保留一下的代码片段,以备日后查用。

BaseThread基类,线程派生类可以继承这个基类,并且实现自己的Run方法。

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Text;
  4 using System.Threading;
  5  
  6 namespace BaseThread
  7 {
  8     public class BaseThread
  9     {
 10         public enum ThreadStatus
 11         {
 12             CREATED,
 13             RUNNING,
 14             STOPED,
 15         };
 16  
 17         private int m_Tid;
 18         private bool m_IsAlive;
 19         private ThreadStatus m_Status;
 20         private Thread m_WorkingThread;
 21  
 22         private static void WorkFunction(object arg)
 23         {
 24             try
 25             {
 26                 System.Diagnostics.Trace.WriteLine("BaseThread::WorkFunction {");
 27                 ((BaseThread)arg).Run();
 28                 System.Diagnostics.Trace.WriteLine("BaseThread::WorkFunction }");
 29             }
 30             catch (ThreadAbortException abt_ex)
 31             {
 32                 System.Diagnostics.Trace.WriteLine("BaseThread::WorkFunction ThreadAbortException " + abt_ex.ToString());
 33                 Thread.ResetAbort();
 34             }
 35             catch (ThreadInterruptedException int_ex)
 36             {
 37                 System.Diagnostics.Trace.WriteLine("BaseThread::WorkFunction ThreadAbortException " + int_ex.ToString());
 38             }
 39             catch (Exception ex)
 40             {
 41                 System.Diagnostics.Trace.WriteLine("BaseThread::WorkFunction Exception " +ex.ToString());
 42             }
 43             finally
 44             {
 45                 ((BaseThread)arg).Status = ThreadStatus.STOPED;
 46                 m_IsAlive = false;
 47                 m_Tid = -1;
 48             }
 49         }
 50  
 51         public BaseThread()
 52         {
 53             m_WorkingThread = new Thread(WorkFunction);
 54             m_Tid = -1;
 55             m_IsAlive = false;
 56             m_Status = ThreadStatus.CREATED;
 57         }
 58  
 59         public int Start()
 60         {
 61             try
 62             {
 63                 System.Diagnostics.Trace.WriteLine("DLx Framework BaseThread Start() {");
 64                 m_WorkingThread.Start(this);
 65                 m_Tid = m_WorkingThread.ManagedThreadId;
 66                 m_IsAlive = true;
 67                 m_Status = ThreadStatus.RUNNING;
 68  
 69                 System.Diagnostics.Trace.WriteLine("DLx Framework BaseThread Start() }");
 70             }
 71             catch(Exception ex)
 72             {
 73                 System.Diagnostics.Trace.WriteLine("DLx Framework BaseThread Start() Get an exception: " + ex.ToString());
 74                 m_Tid = -1;
 75                 m_IsAlive = false;
 76                 m_Status = ThreadStatus.STOPED;
 77             }
 78             return 0;
 79         }
 80  
 81         public int Stop(int timeout)
 82         {
 83             System.Diagnostics.Trace.WriteLine("DLx Framework BaseThread Stop {");
 84             try
 85             {
 86                 if (m_IsAlive)
 87                 {
 88                     System.Diagnostics.Trace.WriteLine("BaseThread Stop: Thread is alive");
 89                     m_IsAlive = false;
 90  
 91                     if (timeout > 0)
 92                     {
 93                         if (!m_WorkingThread.Join(timeout))
 94                         {
 95                             System.Diagnostics.Trace.WriteLine("BaseThread Stop: Join failed, m_WorkingThread.Abort {");
 96                             m_IsAlive = false;
 97                             m_WorkingThread.Abort();
 98                             System.Diagnostics.Trace.WriteLine("BaseThread Stop: Join failed, m_WorkingThread.Abort }");
 99                         }
100                     }
101                     else
102                     {
103                         if (!m_WorkingThread.Join(3000))
104                         {
105                             System.Diagnostics.Trace.WriteLine("BaseThread Stop: Join Timer default = 3000 failed, m_WorkingThread.Abort {");
106                             m_IsAlive = false;
107                             m_WorkingThread.Abort();
108                             System.Diagnostics.Trace.WriteLine("BaseThread Stop: Join Timer default = 3000 failed, m_WorkingThread.Abort }");
109                         }
110                     }
111                 }
112                 else
113                 {
114                     System.Diagnostics.Trace.WriteLine("BaseThread Stop: Thread is NOT alive");
115  
116                     if (m_WorkingThread != null)
117                     {
118                         System.Diagnostics.Trace.WriteLine("BaseThread Stop: Thread status is UNUSUAL");
119                         if (m_WorkingThread.IsAlive)
120                         {
121                             System.Diagnostics.Trace.WriteLine("BaseThread Stop: Force to ABOTR ");
122                             m_WorkingThread.Abort();
123                         }
124                     }
125                 }
126                 m_WorkingThread = null;
127             }
128             catch (Exception ex)
129             {
130                 System.Diagnostics.Trace.WriteLine("BaseThread Stop: " + ex.ToString());
131             }
132  
133             System.Diagnostics.Trace.WriteLine("DLx Framework BaseThread Stop }");
134             return 0;
135         }
136         
137         public int Tid
138         {
139             get { return m_Tid; }    
140         }
141  
142         public bool IsAlive
143         {
144             get { return m_IsAlive; }
145         }
146  
147         public ThreadStatus Status
148         {
149             get { return m_Status; }
150             set { m_Status = value; }
151         }
152  
153  
154         public virtual void Run(){}
155  
156         ~BaseThread()
157         {
158             if (m_WorkingThread != null)
159             {
160                 System.Diagnostics.Trace.WriteLine("~BaseThread: Thread status is UNUSUAL");
161                 if (m_WorkingThread.IsAlive)
162                 {
163                     System.Diagnostics.Trace.WriteLine("~BaseThread: Force to ABOTR ");
164                     m_WorkingThread.Abort();
165                 }
166                 m_WorkingThread = null;
167             }
168         }
169     }
170 }

 

子类继承 BaseThread,它的Run函数很简单,只是打印数字。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5  
 6 using DLxFramework;
 7  
 8  
 9 namespace UseThreadBase
10 {
11     public class Worker : BaseThread
12     {
13         public Worker()
14         {}
15  
16         private int m_i = 0;
17         public override void Run()
18         {
19             while (IsAlive)
20             {
21                 Console.WriteLine("Worker : " + m_i++);
22             }
23         }
24  
25         public int Value
26         {
27             get { return m_i; }
28         }
29     }
30 }

 

Main函数启动和停止工作线程。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5  
 6 namespace UseThreadBase
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             Worker work = new Worker();
13  
14             work.Start();
15  
16             System.Threading.Thread.Sleep(5000);
17  
18             work.Stop(2000);
19  
20         }
21     }
22 }

 

在整个的例子中,业务处理的工作单元并不需要去关注线程的启动,停止和异常处理等细节。它只需要重载Run函数,并添加自己的工作内容即可。

BaseThread中实现了对线程的基本控制。

转载于:https://www.cnblogs.com/neo98/articles/5751226.html

原文地址:https://www.cnblogs.com/XuYiHe/p/14714132.html