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)
|
{
|
}
|
}
|
}
|
}
|