Beispiel #1
0
        public void Stop()
        {
            lock (_lock)
            {
                if (_state == State.Running || _state == State.Suspended)
                {
                    _requestedState = State.Stopped;

                    // Prevent deadlock by checking is we stopping from worker task or not.
                    if (Task.CurrentId != _workerTask.Id)
                    {
                        _eventWaiter.Set();
                        _workerTask.Wait();

                        // http://blogs.msdn.com/b/pfxteam/archive/2012/03/25/10287435.aspx
                        // Actually it's not required to call dispose on task, but we will do this if possible.
                        _workerTask.Dispose();
                    }
                }
                else
                {
                    throw new InvalidOperationException("The worker is already stopped.");
                }
            }
        }
Beispiel #2
0
        public void Stop()
        {
            lock (_lock)
            {
                if (_state == WorkerState.Running || _state == WorkerState.Suspended)
                {
                    _requestedState = WorkerState.Stopped;

                    // Prevent deadlock by checking is we stopping from worker task or not.
                    if (Thread.CurrentThread.ManagedThreadId != _workerTask.ManagedThreadId)
                    {
                        _eventWaiter.Set();
                        _workerTask.Join();
                    }
                }
                else if (!_isFaulted)
                {
                    // Probably not needed, added as a precaution.
                    throw new InvalidOperationException("The worker is already stopped.");
                }
            }
        }
Beispiel #3
0
        /// <summary> Queue the send command wrapped in a command strategy. </summary>
        /// <param name="commandStrategy"> The command strategy. </param>
        public override void QueueCommand(CommandStrategy commandStrategy)
        {
            while (Queue.Count > MaxQueueLength)
            {
                Thread.Yield();
            }
            lock (Queue)
            {
                // Process commandStrategy enqueue associated with command
                commandStrategy.CommandQueue   = Queue;
                commandStrategy.ThreadRunState = ThreadRunState;

                commandStrategy.Enqueue();

                // Process all generic enqueue strategies
                foreach (var generalStrategy in GeneralStrategies)
                {
                    generalStrategy.OnEnqueue();
                }
            }
            EventWaiter.Set();
        }