Exemplo 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);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Allows the caller to notify the SyncGate that it wants exclusive or shared access to a resource.
        /// </summary>
        /// <param name="mode">Indicates if exclusive or shared access is desired.</param>
        /// <param name="asyncCallback">The callback method to invoke once access can be granted.</param>
        /// <param name="asyncState">Additional state to pass to the callback method.</param>
        public void BeginRegion(SyncGateMode mode, ChoAsyncCallback asyncCallback, object asyncState)
        {
            ChoGuard.ArgumentNotNull(asyncCallback, "asyncCallback");

            SyncGateAsyncResult ar = new SyncGateAsyncResult(mode, asyncCallback, asyncState);

            lock (_syncLock)
            {
                switch (mode)
                {
                case SyncGateMode.Exclusive:
                    switch (_state)
                    {
                    case SyncGateStates.OwnedByReaders:
                    case SyncGateStates.OwnedByReadersAndWriterPending:
                        _state = SyncGateStates.OwnedByReadersAndWriterPending;
                        _qWriteRequests.Enqueue(ar);
                        break;

                    case SyncGateStates.OwnedByWriter:
                        _qWriteRequests.Enqueue(ar);
                        break;
                    }
                    break;

                case SyncGateMode.Shared:
                    switch (_state)
                    {
                    case SyncGateStates.Free:
                    case SyncGateStates.OwnedByReaders:
                        _state = SyncGateStates.OwnedByReaders;
                        _numReaders++;
                        QueueCallback(ar);
                        break;

                    case SyncGateStates.OwnedByReadersAndWriterPending:
                    case SyncGateStates.OwnedByWriter:
                    case SyncGateStates.ReservedForWriter:
                        _qReadRequests.Enqueue(ar);
                        break;
                    }
                    break;
                }
            }
        }
        public override IChoAbortableAsyncResult EnqueueMethod(Delegate func, object[] parameters, ChoAsyncCallback callback, object state, int timeout, int maxNoOfRetry, int sleepBetweenRetry)
        {
            ChoGuard.ArgumentNotNull(func, "Function");
            CheckTimeoutArg(timeout);

            ChoExecutionServiceData data = new ChoExecutionServiceData(func, parameters, timeout, new ChoAbortableAsyncResult(callback, state), maxNoOfRetry, sleepBetweenRetry);

            _queuedMsgService.Enqueue(ChoStandardQueuedMsgObject <ChoExecutionServiceData> .New(data));

            return(data.Result as IChoAbortableAsyncResult);
        }
Exemplo n.º 4
0
        public override IChoAsyncResult EnqueueMethod(Delegate func, object[] parameters, ChoAsyncCallback callback, object state, int timeout, int maxNoOfRetry, int sleepBetweenRetry)
        {
            ChoGuard.ArgumentNotNull(func, "Function");
            CheckTimeoutArg(timeout);

            if (ChoGuard.IsDisposed(this))
            {
                return(null);
            }

            timeout           = timeout > _timeout ? _timeout : timeout;
            maxNoOfRetry      = maxNoOfRetry > _maxNoOfRetry ? _maxNoOfRetry : maxNoOfRetry;
            sleepBetweenRetry = sleepBetweenRetry > _sleepBetweenRetry ? _sleepBetweenRetry : sleepBetweenRetry;

            ChoExecutionServiceData data = new ChoExecutionServiceData(func, parameters, timeout, new ChoAsyncResult(callback, state), maxNoOfRetry, sleepBetweenRetry);

            _queuedMsgService.Enqueue(ChoStandardQueuedMsgObject <ChoExecutionServiceData> .New(data));

            return(data.Result);
        }
        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(ChoConsolePercentageProgressorStartEx consolePercentageProgressorStart, ChoAsyncCallback callback, object state)
 {
     Start(consolePercentageProgressorStart, callback, state, Timeout.Infinite);
 }
        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);
        }
Exemplo n.º 8
0
 internal ChoAbortableAsyncResult(ChoAsyncCallback asyncCallback, Object state)
     : base(state)
 {
     _asyncCallback = asyncCallback;
 }
Exemplo n.º 9
0
 /// <summary>
 /// Allows the caller to notify the SyncGate that it wants exclusive or shared access to a resource.
 /// </summary>
 /// <param name="mode">Indicates if exclusive or shared access is desired.</param>
 /// <param name="asyncCallback">The callback method to invoke once access can be granted.</param>
 public void BeginRegion(SyncGateMode mode, ChoAsyncCallback asyncCallback)
 {
     BeginRegion(mode, asyncCallback, null);
 }
Exemplo n.º 10
0
 internal SyncGateAsyncResult(SyncGateMode mode, ChoAsyncCallback asyncCallback, object state)
     : base(asyncCallback, state)
 {
     this._syncGateMode = mode;
 }