/// <summary> /// Create conrete thread by ThreadFactory /// </summary> /// <param name="ThreadName"></param> /// <param name="_tokenSource"></param> /// <param name="_threadParameter"></param> public ThreadContext(PINThreadName ThreadName, CancellationTokenSource _tokenSource, ICloneable _threadParameter) { superThread = ThreadFactory.createThread(ThreadName, _tokenSource, _threadParameter); }
/// <summary> /// Set buttons are enabled or not. /// </summary> /// <param name="isEnabled">true:Button are enabled; false: buttons are disenabled.</param> private void SetButtonStatus(PINThreadName ThreadName, bool flag) { switch (ThreadName) { case PINThreadName.GetNumber: this.SetGetNumberButtonStatus(flag); break; case PINThreadName.DigitInitial: this.SetDigitInitialButtonStatus(flag); break; default: break; } }
/// <summary> /// Create different concrete thread by different ThreadName /// </summary> public static PINThread createThread(PINThreadName ThreadName, CancellationTokenSource _tokenSource, ICloneable _threadParameter) { PINThread thread = null; switch (ThreadName) { case PINThreadName.GetNumber: thread = new ThreadGetNumber(_tokenSource, _threadParameter); break; case PINThreadName.DigitInitial: thread = new ThreadDigitInitial(_tokenSource, _threadParameter); break; default: break; } return(thread); }
/// <summary> /// get parameter /// </summary> /// <param name="ThreadName"></param> /// <returns></returns> private ICloneable GetParameter(PINThreadName ThreadName) { ICloneable param = null; switch (ThreadName) { case PINThreadName.GetNumber: param = this.GetGetNumberParameter(); break; case PINThreadName.DigitInitial: param = this.GetDigitInitialParameter(); break; default: break; } return(param); }
/// <summary> /// Execute a thread /// </summary> /// <param name="ThreadName"></param> /// <returns></returns> private async Task <int> RunThread(PINThreadName ThreadName) { DateTime dt1, dt2; dt1 = System.DateTime.Now; int rtn = -1; try { //Set buttons enabled to be disenable this.SetButtonStatus(ThreadName, false); CancellationTokenSource tokenSource = StartNewTask(); //Get threadparameter ICloneable param = GetParameter(ThreadName); //Get context for to execute thread ThreadContext threadContext = new ThreadContext(ThreadName, tokenSource, param); //Thread running task = threadContext.ThreadRun(); // wait thread is over rtn = await task; } catch (Exception ex) { LoggerHelper.Error("Task<int> RunThread Function", ex); } finally { //Set buttons enabled from disabled to enabled. this.SetButtonStatus(ThreadName, true); dt2 = System.DateTime.Now; if (SystemConfiguration.Debug) { LoggerHelper.Debug("[" + ThreadName.ToString() + "] is finished. It took " + (dt2 - dt1).ToString() + "\r\n"); } } return(rtn); }