Exemple #1
0
        public ProgressBar(int maxTicks, string message, ProgressBarOptions options = null)
            : base(maxTicks, message, options)
        {
            _originalCursorTop    = Console.CursorTop;
            _originalWindowTop    = Console.WindowTop;
            _originalWindowHeight = Console.WindowHeight + _originalWindowTop;
            _originalColor        = Console.ForegroundColor;

            _writeMessageToConsole = this.Options.WriteQueuedMessage ?? DefaultConsoleWrite;

            if (!Console.IsOutputRedirected)
            {
                Console.CursorVisible = false;
            }

            if (this.Options.EnableTaskBarProgress)
            {
                TaskbarProgress.SetState(TaskbarProgress.TaskbarStates.Normal);
            }

            if (this.Options.DisplayTimeInRealTime)
            {
                _timer = new Timer((s) => OnTimerTick(), null, 500, 500);
            }
            else             //draw once
            {
                _timer = new Timer((s) =>
                {
                    _timer.Dispose();
                    DisplayProgress();
                }, null, 0, 1000);
            }

            _displayProgressEvent = new AutoResetEvent(false);
            _displayProgress      = Task.Run(() =>
            {
                while (_isDisposed == 0)
                {
                    if (!_displayProgressEvent.WaitOne(TimeSpan.FromSeconds(10)))
                    {
                        continue;
                    }
                    try
                    {
                        UpdateProgress();
                    }
                    catch
                    {
                        // don't want to crash background thread
                    }
                }
            });
        }
        public ProgressBar(int maxTicks, string message, ProgressBarOptions options = null)
            : base(maxTicks, message, options)
        {
            _originalCursorTop = Console.CursorTop;
            _originalWindowTop = Console.WindowTop;
            _originalColor     = Console.ForegroundColor;

            Console.CursorVisible = false;

            if (this.Options.EnableTaskBarProgress)
            {
                TaskbarProgress.SetState(TaskbarProgress.TaskbarStates.Normal);
            }

            if (this.Options.DisplayTimeInRealTime)
            {
                _timer = new Timer((s) => DisplayProgress(), null, 500, 500);
            }
            else             //draw once
            {
                _timer = new Timer((s) =>
                {
                    _timer.Dispose();
                    DisplayProgress();
                }, null, 0, 1000);
            }

            _displayProgressEvent = new AutoResetEvent(false);
            _displayProgress      = Task.Run(() =>
            {
                while (_isDisposed == 0)
                {
                    _displayProgressEvent.WaitOne();

                    try
                    {
                        UpdateProgress();
                    }
                    catch
                    {
                        // don't want to crash background thread
                    }
                }
            });
        }
Exemple #3
0
        public void Dispose()
        {
            if (this.EndTime == null)
            {
                this.EndTime = DateTime.Now;
            }
            var openDescendantsPadding = (_visisbleDescendants * 2);

            if (this.Options.EnableTaskBarProgress)
            {
                TaskbarProgress.SetState(TaskbarProgress.TaskbarStates.NoProgress);
            }

            try
            {
                var moveDown         = 0;
                var currentWindowTop = Console.WindowTop;
                if (currentWindowTop != _originalWindowTop)
                {
                    var x = Math.Max(0, Math.Min(2, currentWindowTop - _originalWindowTop));
                    moveDown = _originalCursorTop + x;
                }
                else
                {
                    moveDown = _originalCursorTop + 2;
                }

                Console.CursorVisible = true;
                Console.CursorLeft    = 0;
                Console.CursorTop     = (openDescendantsPadding + moveDown);
            }
            // This is bad and I should feel bad, but i rather eat pbar exceptions in productions then causing false negatives
            catch
            {
            }
            Console.WriteLine();
            _isDisposed = true;
            _timer?.Dispose();
            _timer = null;
            foreach (var c in this.Children)
            {
                c.Dispose();
            }
        }
Exemple #4
0
        public void Dispose()
        {
            if (Interlocked.CompareExchange(ref _isDisposed, 1, 0) != 0)
            {
                return;
            }

            _timer?.Dispose();
            _timer = null;

            // make sure background task is stopped before we clean up
            _displayProgressEvent.Set();
            _displayProgress.Wait();

            // update one last time - needed because background task might have
            // been already in progress before Dispose was called and it might
            // have been running for a very long time due to poor performance
            // of System.Console
            UpdateProgress();

            //make sure we pop all pending messages
            while (_stickyMessages.TryDequeue(out var m))
            {
                WriteConsoleLine(m);
            }

            if (this.EndTime == null)
            {
                this.EndTime = DateTime.Now;
            }

            if (this.Options.EnableTaskBarProgress)
            {
                TaskbarProgress.SetState(TaskbarProgress.TaskbarStates.NoProgress);
            }

            try
            {
                foreach (var c in this.Children)
                {
                    c.Dispose();
                }
            }
            catch
            {
            }

            try
            {
                var openDescendantsPadding = (_visibleDescendants * 2);
                var newCursorTop           = Math.Min(_originalWindowHeight, _originalCursorTop + 2 + openDescendantsPadding);
                Console.CursorVisible = true;
                Console.SetCursorPosition(0, newCursorTop);
            }
            //This is bad and I should feel bad, but i rather eat pbar exceptions in production then causing false negatives
            catch
            {
            }

            //Console.WriteLine();
        }