protected override void Dispose(bool finalize) { if (_result != null) { try { _result.EndInvoke(); } catch (ChoFatalApplicationException) { throw; } catch (Exception ex) { ErrorOccured.Raise(this, new ChoExceptionEventArgs(ex)); } finally { _result = null; IChoAsyncResult result = ChoConsole.OutputQueuedExecutionService.Enqueue(() => { //Reset to old settings Console.ForegroundColor = ChoConsole.DefaultConsoleForegroundColor; Console.BackgroundColor = ChoConsole.DefaultConsoleBackgroundColor; Console.SetCursorPosition(0, _statusMsgLocation.Y + 1); Console.WriteLine(); }); result.AsyncWaitHandle.WaitOne(); } } }
public void Cancel() { if (!_isRunning) { return; } try { IChoAbortableAsyncResult r1 = _result; if (r1 == null) { return; } if (_topPlugIn != null) { _topPlugIn.Stop(); } Thread.Sleep(_plugInManagerSettings.StopRequestTimeout); r1.Abort(); } finally { _result = null; } }
public void Reset() { Verify(); _topPlugIn = _nextPlugIn = null; _result = null; _isRunning = false; }
public void Start(ChoConsoleProgressorStart consoleProgressorStart, ChoAsyncCallback callback, object state, int timeout) { ChoGuard.NotDisposed(this); int isStarted = Interlocked.CompareExchange(ref _isStarted, 1, 0); if (isStarted == 1) { return; } Interlocked.CompareExchange(ref _stopRequested, 0, 1); lock (ChoConsole.SyncRoot) { ChoConsole.Clear(); ChoConsole.ClearKeys(); IChoAsyncResult result = ChoConsole.OutputQueuedExecutionService.Enqueue(() => { _location = WriteNSavePosition(_msg + " "); _statusMsgLocation = new ChoPoint(_consolePercentageProgressorSettings.ProgressBarMarginX, _location.Y + 1); WriteSpinner("/"); }); result.AsyncWaitHandle.WaitOne(); } Action <ChoConsoleSpinProgressor> wrappedFunc = delegate { _threadToKill = Thread.CurrentThread; try { while (true) { if (_stopRequested == 1) { break; } ChoConsole.WriteLine(GetNextSpinChar().ToString(), _location, _foregroundColor, _backgroundColor); bool retValue = consoleProgressorStart(this, state); if (!retValue) { break; } } } catch (ThreadAbortException) { Thread.ResetAbort(); } }; _result = ChoAbortableQueuedExecutionService.Global.Enqueue <ChoConsoleSpinProgressor>(wrappedFunc, this); }
public object Run(object value, object contextInfo = null) { RunAsync(value, contextInfo, null); try { IChoAbortableAsyncResult result = _result; if (result != null) { return(result.EndInvoke()); } } finally { OnPlugInRunComplete(_result); _result = null; } return(null); }
private void RunAsync(object value, object contextInfo = null, ChoAbortableAsyncCallback callback = null) { Verify(); if (_topPlugIn == null) { return; } ActivePlugIn = null; _result = ChoAbortableQueuedExecutionService.Global.Enqueue(() => { if (_topPlugIn == null) { return(null); } _isRunning = true; //lock (_padLock) //{ try { _topPlugIn.ContextInfo = contextInfo; _topPlugIn.BeforeRun += OnBeforePlugInRun; _topPlugIn.AfterRun += OnAfterPlugInRun; return(_topPlugIn.Run(value)); } finally { //_topPlugIn.BeforeRun -= BeforePlugInRun; } //} }, callback, null, -1); }
public void Start(ChoConsolePercentageProgressorStartEx consolePercentageProgressorStart, ChoAsyncCallback callback, object state, int timeout) { ChoGuard.NotDisposed(this); int isStarted = Interlocked.CompareExchange(ref _isStarted, 1, 0); if (isStarted == 1) { return; } Interlocked.CompareExchange(ref _stopRequested, 0, 1); lock (ChoConsole.SyncRoot) { ChoConsole.Clear(); ChoConsole.ClearKeys(); _consoleCoordinate = new ConsoleCoordinate((short)(Console.CursorLeft + _consolePercentageProgressorSettings.ProgressBarMarginX), (short)(Console.CursorTop + _consolePercentageProgressorSettings.ProgressBarMarginY)); ShowProgress(MinPercentage, _msg); } Action <ChoConsolePercentageProgressorEx, int> wrappedFunc = delegate { _threadToKill = Thread.CurrentThread; try { int percentage = MinPercentage; int retPercentage = MinPercentage; while (retPercentage < MaxPercentage) { if (_stopRequested == 1) { break; } Tuple <int, string> retValue = consolePercentageProgressorStart(this, percentage, state); retPercentage = retValue.Item1; if (percentage >= retPercentage) { throw new ChoConsoleException("Returned percentage '{0}' value <= running percentage '{1}' value. It may leads to infinite loop.".FormatString(retPercentage, percentage)); } else { percentage = retPercentage; } ShowProgress(retPercentage, retValue.Item2); } } catch (ThreadAbortException) { Thread.ResetAbort(); } finally { Console.WriteLine(); Console.WriteLine(); Console.ResetColor(); } }; _result = ChoAbortableQueuedExecutionService.Global.Enqueue <ChoConsolePercentageProgressorEx, int>(wrappedFunc, this, MinPercentage, callback, state, timeout); }
public void Start(ChoConsolePercentageProgressorStart consolePercentageProgressorStart, ChoAsyncCallback callback, object state, int timeout) { ChoGuard.NotDisposed(this); //if (_isStarted) return; int isStarted = Interlocked.CompareExchange(ref _isStarted, 1, 0); if (isStarted == 1) { return; } Interlocked.CompareExchange(ref _stopRequested, 0, 1); lock (ChoConsole.SyncRoot) { ChoConsole.Clear(); ChoConsole.ClearKeys(); IChoAsyncResult result = ChoConsole.OutputQueuedExecutionService.Enqueue(() => { _location = WriteNSavePosition(_msg + " "); _statusMsgLocation = new ChoPoint(_consolePercentageProgressorSettings.ProgressBarMarginX, _location.Y + 1); WritePercentage("[0%]"); }); result.AsyncWaitHandle.WaitOne(); SetPercentageComplete(MinPercentage); } Action <ChoConsolePercentageProgressor, int> wrappedFunc = delegate { _threadToKill = Thread.CurrentThread; try { int percentage = MinPercentage; int retPercentage = MinPercentage; while (retPercentage < MaxPercentage) { if (_stopRequested == 1) { break; } retPercentage = consolePercentageProgressorStart(this, percentage, state); if (percentage >= retPercentage) { throw new ChoConsoleException("Returned percentage '{0}' value <= running percentage '{1}' value. It may leads to infinite loop.".FormatString(retPercentage, percentage)); } else { percentage = retPercentage; } lock (ChoConsole.SyncRoot) { SetPercentageComplete(retPercentage); } } } catch (ThreadAbortException) { Thread.ResetAbort(); } }; //try //{ // ChoAPM.InvokeMethod(wrappedFunc, new object[] { this, MinPercentage }, timeout); //} //catch (Exception ex) //{ // ErrorOccured(this, new ChoExceptionEventArgs(ex)); //} _result = ChoAbortableQueuedExecutionService.Global.Enqueue <ChoConsolePercentageProgressor, int>(wrappedFunc, this, MinPercentage, callback, state, timeout); }
private void OnPlugInRunComplete(IChoAbortableAsyncResult result) { _isRunning = false; RunComplete.Raise(this, new ChoEventArgs <IChoAbortableAsyncResult>(result)); }