wzp
2021-07-28 864986e4cad03f6b9bba9a7e65379db496b62a6a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
namespace AsiaINFO.SMS.APPCMPP2.MyThread
{
    using AsiaINFO.SMS.APPCMPP2;
    using AsiaINFO.SMS.CMPP2;
    using System;
    using System.Runtime.CompilerServices;
    using System.Threading;
 
    public abstract class ThreadBase
    {
        protected SyncEvents _syncEvents;
        protected object _syncSuspendLogLockObject;
        protected Thread _thread;
 
        public event AsiaINFO.SMS.APPCMPP2.MyThread.MsgEventDelegate MsgEvent;
 
        public event AsiaINFO.SMS.APPCMPP2.MyThread.ThreadStateDelegate ThreadStateEvent;
 
        public ThreadBase(SyncEvents syncEvents)
        {
            if (syncEvents == null)
            {
                throw new Exception("syncEvents参数不能为空!");
            }
            this._syncEvents = syncEvents;
            this._syncSuspendLogLockObject = new object();
        }
 
        public bool IsStop()
        {
            if (((this._thread != null) && (this._thread.ThreadState != ThreadState.Stopped)) && ((this._thread.ThreadState != ThreadState.Aborted) && (this._thread.ThreadState != ThreadState.AbortRequested)))
            {
                return false;
            }
            return true;
        }
 
        protected void OnMsg(string msg, MsgLevel level)
        {
            if (this.MsgEvent != null)
            {
                this.MsgEvent(msg, level);
            }
        }
 
        protected void OnThreadState(ThreadState state)
        {
            if (this.ThreadStateEvent != null)
            {
                this.ThreadStateEvent(this, state);
            }
        }
 
        protected virtual void Run()
        {
        }
 
        public void SetThreadPriority(ThreadPriority threadPriority)
        {
            this._thread.Priority = threadPriority;
        }
 
        public void StartRun()
        {
            try
            {
                if (this._thread == null)
                {
                    this._thread = new Thread(new ThreadStart(this.Run));
                }
                if (this._thread.ThreadState == ThreadState.Unstarted)
                {
                    this.OnThreadState(ThreadState.Running);
                    this._thread.Start();
                    this._syncEvents.NewItemEvent.Set();
                }
                else if (this._thread.ThreadState == ThreadState.Suspended)
                {
                    this.OnThreadState(ThreadState.Running);
                    this._thread.Resume();
                    this._syncEvents.NewItemEvent.Set();
                }
                else if (this._thread.ThreadState == ThreadState.Stopped)
                {
                    this.OnThreadState(ThreadState.Running);
                    this._thread = null;
                    this._thread = new Thread(new ThreadStart(this.Run));
                    this._thread.Start();
                    this._syncEvents.NewItemEvent.Set();
                }
               
            }
            catch (Exception exception)
            {
                this.OnMsg("线程启动错误" + exception.Message, MsgLevel.Err);
            }
        }
 
        public virtual void Stop()
        {
            try
            {
                if (this._thread != null)
                {
                    this._thread.Abort();
                }
            }
            catch (ThreadAbortException)
            {
            }
            catch (Exception)
            {
            }
            finally
            {
                this.OnThreadState(ThreadState.Stopped);
            }
        }
 
        public void StopImmediately()
        {
            try
            {
                if (this._thread != null)
                {
                    this._thread.Abort();
                }
            }
            catch (ThreadAbortException)
            {
            }
            catch (Exception)
            {
            }
            finally
            {
                this.OnThreadState(ThreadState.Stopped);
            }
        }
 
        public void Suspend()
        {
            try
            {
                if (((this._thread != null) && (this._thread.ThreadState != ThreadState.Stopped)) && (this._thread.ThreadState != ThreadState.Suspended))
                {
                    this.OnThreadState(ThreadState.Suspended);
                    lock (this._syncSuspendLogLockObject)
                    {
                        this._thread.Suspend();
                    }
                }
            }
            catch (Exception)
            {
            }
        }
    }
}