Exemplo n.º 1
0
        public IDisposable Start(bool displayIndicatorImmediatley = false)
        {
            if (_parentControl.InvokeIfRequired(() => Start(displayIndicatorImmediatley)))
            {
                return(_stopDisposable);
            }

            if (Interlocked.Increment(ref _started) != 1)
            {
                return(_stopDisposable);
            }

            _indicatorShownAt = null;

            // HACK: To capture latest screenshot
            // i.e. when we select node in tree view just before start operation
            // without refresh this selection will be displayed only after layer control will disappear
            _parentControl.Refresh();

            _previouslyFocusedControl = FindFocusedControl(_parentControl.FindForm());

            var parentControlImage = _parentControl.CaptureScreenshot();

            _layerControl          = new LayerControl(parentControlImage, _settings.ProcessImage);
            _layerControl.Location = new Point(0, 0);
            _layerControl.Size     = _parentControl.Size;
            _layerControl.Anchor   = AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right | AnchorStyles.Top;

            _parentControl.Controls.Add(_layerControl);

            _layerControl.SubscribeChildrenControlEnter();
            _layerControl.BringToFront();

            _layerControl.SafeSelect();

            _cancelationSource = new CancellationTokenSource();

            if (displayIndicatorImmediatley)
            {
                DisplayIndicator(_cancelationSource.Token);
            }
            else
            {
                Task.Run(StartAsync);
            }

            return(_stopDisposable);
        }
Exemplo n.º 2
0
        private void StopInternal()
        {
            if (_parentControl.InvokeIfRequired(StopInternal))
            {
                if (_parentControl.IsDisposed || _parentControl.Disposing)
                {
                    var layerControl = _layerControl;
                    layerControl?.Remove();
                }

                return;
            }

            var value = Interlocked.Decrement(ref _started);

            if (value > 0)
            {
                return;
            }

            if (value < 0)
            {
                if (_settings.AllowStopBeforeStart)
                {
                    return;
                }

                throw new InvalidOperationException("Stop long operation more times then starts.");
            }

            _cancelationSource.Cancel();

            var form           = _parentControl.FindForm();
            var currentFocused = FindFocusedControl(form);

            _layerControl?.Remove();

            if (form != null && currentFocused == _layerControl)
            {
                RestoreFocus(form);
            }

            _layerControl = null;
        }