Ejemplo n.º 1
0
        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);
        }
        private ChoPoint WriteNSavePosition(string msg)
        {
            //Save the previous settings
            ConsoleColor prevForegroundColor = Console.ForegroundColor;
            ConsoleColor prevBackgroundColor = Console.BackgroundColor;

            //Set the passed settings
            Console.ForegroundColor = _foregroundColor;
            Console.BackgroundColor = _backgroundColor;

            Console.SetCursorPosition(Console.CursorLeft + _consolePercentageProgressorSettings.ProgressBarMarginX,
                                      Console.CursorTop + _consolePercentageProgressorSettings.ProgressBarMarginY);

            Console.Write(msg);
            ChoPoint currentLocation = new ChoPoint(Console.CursorLeft, Console.CursorTop);

            //Reset to old settings
            Console.ForegroundColor = prevForegroundColor;
            Console.BackgroundColor = prevBackgroundColor;

            return(currentLocation);
        }
        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);
        }
Ejemplo n.º 4
0
 public ChoConsoleSession(ChoPoint cursorLocation, ConsoleColor foregroundColor, ConsoleColor backgroundColor)
     : this(cursorLocation.X, cursorLocation.Y, Console.ForegroundColor, Console.BackgroundColor)
 {
 }
Ejemplo n.º 5
0
 public ChoConsoleSession(ChoPoint cursorLocation)
     : this(cursorLocation.X, cursorLocation.Y)
 {
 }