| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- 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;
- }
- }
- }
|