using System; using System.Collections.Generic; using System.Text; using System.Threading; namespace ApiProcess.Process { public abstract class MainThread { //Cac thuoc tinh cua Thead private const string PROCESSING_THREAD_NAME = "ProcThread"; private const byte PROC_INITIALISING = 0; private const byte PROC_RECEIVING = 1; private const byte PROC_FINISHED = 2; private object _ProcessingStatusLock = new object(); private byte _ProcessingStatus = PROC_INITIALISING; private int _ThreadIndex = 0; private bool _KeepProcessing = true; private Exception _TermException = null; private Thread _ProcessingThread = null; private bool _ThreadSleeping = false; public abstract void Process(); //Cac phuong thuc cua thread public virtual void Start(string _name) { if (!IsProcessing()) { // i.e. is initialising or finished SetProcessingStatus(PROC_INITIALISING); _TermException = null; _KeepProcessing = true; //To name a delegate. ThreadStart ts = new ThreadStart(Run); _ProcessingThread = new Thread(ts); _ProcessingThread.Name = _name; _ProcessingThread.Start(); while (IsInitialising()) { Sleep(30); } } }//End of Start by name public virtual void Start() { if (!IsProcessing()) { SetProcessingStatus(PROC_INITIALISING); _TermException = null; _KeepProcessing = true; ThreadStart ts = new ThreadStart(Run); _ProcessingThread = new Thread(ts); _ProcessingThread.Name = GenerateDefaultIndexedThreadName(); _ProcessingThread.Start(); while (IsInitialising()) { Sleep(10); } } }//End of Start public virtual void Stop(int timeout) { if (IsProcessing()) { StopProcessing(null); if (_ProcessingThread.ThreadState == ThreadState.WaitSleepJoin) { _ProcessingThread.Interrupt(); } DateTime dt = DateTime.Now; while (!IsFinished()) { TimeSpan tp = DateTime.Now - dt; if (tp.TotalMilliseconds < timeout) { Sleep(10); } else { _ProcessingThread.Abort(); } } } } public virtual void Stop() { if (IsProcessing()) { StopProcessing(null); if (_ProcessingThread.ThreadState == ThreadState.WaitSleepJoin) { _ProcessingThread.Interrupt(); } while (!IsFinished()) { Sleep(10); } } } protected void StopProcessing(Exception e) { SetTermException(e); _KeepProcessing = false; } public void Run() { try { SetProcessingStatus(PROC_RECEIVING); while (_KeepProcessing) { try { Process(); } catch (Exception e1) { Console.WriteLine(e1.ToString()); _KeepProcessing = false; } Sleep(10); } } catch (Exception e) { SetTermException(e); } finally { SetProcessingStatus(PROC_FINISHED); //debug_.exit(DUTL,this); } } public string GenerateDefaultIndexedThreadName() { return GetDefaultThreadName() + "-" + GetThreadIndex(); } public virtual string GetThreadName() { return _ProcessingThread.Name; } public string GetDefaultThreadName() { return PROCESSING_THREAD_NAME; } public int GetThreadIndex() { return ++_ThreadIndex; } public int GetSystemCurrentThreadID() { return AppDomain.GetCurrentThreadId(); } protected void SetTermException(Exception e) { _TermException = e; } public Exception GetTermException() { return _TermException; } private void SetProcessingStatus(byte value) { lock (_ProcessingStatusLock) { _ProcessingStatus = value; } } private bool IsInitialising() { lock (_ProcessingStatusLock) { return _ProcessingStatus == PROC_INITIALISING; } } public bool IsProcessing() { lock (_ProcessingStatusLock) { return _ProcessingStatus == PROC_RECEIVING; } } public bool IsFinished() { lock (_ProcessingStatusLock) { return _ProcessingStatus == PROC_FINISHED; } } public void Sleep(int timeout) { try { _ThreadSleeping = true; Thread.Sleep(timeout);//wait 1 minute _ThreadSleeping = false; } catch (ThreadInterruptedException e) { _ThreadSleeping = false; //Console.WriteLine(e.Message); } } public bool IsSleeping() { return _ThreadSleeping; } } }