using System; using System.Windows.Forms; using System.Threading; using System.Reflection; using System.ComponentModel; using System.Threading.Tasks; //using System.Runtime.ConstrainedExecution; namespace LAPSOFT { public class LThread2 : BackgroundWorker { private ManualResetEvent intervalManualReset; private enum ProcessStatus { Created, Running, JobCompleted, ExceptionOccured }; private ProcessStatus processStatus = new ProcessStatus(); public Boolean bwIsRun; public int time_delay = 0; public int Interval { get; set; } public LThread2() { this.processStatus = ProcessStatus.Created; this.WorkerSupportsCancellation = true; this.Interval = 1000; } private static object lockObjectTienTrinh = new object(); protected override void OnRunWorkerCompleted(RunWorkerCompletedEventArgs e) { base.OnRunWorkerCompleted(e); if (processStatus == ProcessStatus.ExceptionOccured) processStatus = ProcessStatus.JobCompleted; } private static int RandNumber(int Low, int High) { Random rndNum = new Random(int.Parse(Guid.NewGuid().ToString().Substring(0, 8), System.Globalization.NumberStyles.HexNumber)); int rnd = rndNum.Next(Low, High); return rnd; } public delegate void startDelegate(string ID); public event startDelegate StartedEvent; protected override void OnDoWork(DoWorkEventArgs e) { while (!this.CancellationPending) { try { base.OnDoWork(e); StartedEvent(RandNumber(100, 10000).ToString()); Application.DoEvents(); Thread.Sleep((time_delay == 0 ? this.Interval : time_delay)); } catch (Exception exception) { } } if (e != null) e.Cancel = true; } public void Start() { bwIsRun = true; this.processStatus = ProcessStatus.Running; if (this.IsBusy) return; this.intervalManualReset = new ManualResetEvent(false); this.RunWorkerAsync((time_delay == 0 ? this.Interval : time_delay)); } public void Stop() { bwIsRun = false; this.CancelAsync(); this.WakeUp(); this.Dispose(true); } public void WakeUp() { if (this.intervalManualReset != null) this.intervalManualReset.Set(); } private void Sleep() { if (this.intervalManualReset != null) { this.intervalManualReset.Reset(); this.intervalManualReset.WaitOne((time_delay == 0 ? this.Interval : time_delay)); } } public void Activate() { if (!this.IsBusy) // Log ... this.Start(); } } public class LThreadx : BackgroundWorker { #region Members public delegate void startDelegate(string ID); public event startDelegate StartedEvent; private AutoResetEvent _resetEvent = new AutoResetEvent(false); private static int RandNumber(int Low, int High) { Random rndNum = new Random(int.Parse(Guid.NewGuid().ToString().Substring(0, 8), System.Globalization.NumberStyles.HexNumber)); int rnd = rndNum.Next(Low, High); return rnd; } protected override void OnDoWork(DoWorkEventArgs e) { if (!this.IsBusy) { StartedEvent(RandNumber(100, 10000).ToString()); //put whatever parameter suits you or nothing base.OnDoWork(e); e.Result = e.Argument; } } // Main thread sets this event to stop worker thread: public Boolean bwIsRun; #endregion #region Functions public void Start() { try { bwIsRun = true; if (this.IsBusy) { this.CancelAsync(); // wait for the bgw to finish, so it can be reused. _resetEvent.WaitOne(); // will block until _resetEvent.Set() call made } if (!this.IsBusy) this.RunWorkerAsync(); } catch { } } public void Stop() { try { bwIsRun = false; if (!this.IsBusy) this.CancelAsync(); } catch { } } #endregion } }