private void MonitorProcess(object sparam) { //Wait until runner thread finished. this.Wait(-1); this.State = ThreadHelperState.Completed; }
private void Initialize() { this.State = ThreadHelperState.NotStart; threadProcStarter = new ParameterizedThreadStart(this.Runner); threadProcess = new Thread(threadProcStarter); threadMoniStarter = new ParameterizedThreadStart(this.MonitorProcess); threadMonitor = new Thread(threadMoniStarter); }
/// <summary> /// Start the thread with a standard parameters object which help passing your params. /// </summary> /// <param name="sparam">Standard sparam</param> public void Run(StandardParameters sparam) { if (this.Process != null) { if (sparam.CancelSignal != null) this.CancelSignal=sparam.CancelSignal; threadProcess.Start(sparam); threadMonitor.Start(); this.State = ThreadHelperState.Processing; } }
/// <summary> /// Wait until thread finished with specified timeout. /// </summary> /// <param name="millisecond">Millisecond timeout, -1 means infinite.</param> public void Wait(long millisecond) { DateTime startDT= DateTime.Now; while (true) { Application.DoEvents(); DateTime curDT= DateTime.Now; if (threadProcess.IsAlive == false) { this.State = ThreadHelperState.Completed; break; } if (millisecond != -1 && (curDT.Ticks - startDT.Ticks) >= millisecond * 10000) { break; } } }
/// <summary> /// Cancel the thread. /// </summary> public void Cancel(int millisecond) { if (this.CancelSignal != null) this.CancelSignal.Set(); threadProcess.Join(millisecond); // Fire beforeAbort event before abort. if (BeforeAbort != null) { BeforeAbort.Invoke(this, new EventArgs()); } threadProcess.Abort(); this.State = ThreadHelperState.Cancel; }