public async Task MoveToState(TState state, StateTransitionOption option = StateTransitionOption.Default) { if (!IsEnabled) { return; } var flag = true; queueMonitor.Enter(); if (machine.Monitor.TryEnter()) { if (queueCount == 0) { queueMonitor.Exit(); flag = false; try { await machine.MoveToStateInternal(state, option); } finally { queueMonitor.Enter(); if (queueCount > 0) { queueMonitor.Exit(); var _ = StartQueueIfNecessaryAsync(true); // Should not exit monitor here. Its handled by the process queue. } else { queueMonitor.Exit(); machine.Monitor.Exit(); } } } } if (flag) { var tcs = new TaskCompletionSource <bool>(); actionsQueue = actionsQueue.Enqueue(async() => { try { await machine.MoveToStateInternal(state, option); tcs.TrySetResult(true); } catch (Exception ex) { tcs.SetException(ex); } }); queueCount++; queueMonitor.Exit(); var _ = StartQueueIfNecessaryAsync(); await tcs.Task; } }
public Task FireAsync <TArgument>(ParameterizedTrigger <TTrigger, TArgument> parameterizedTrigger, TArgument argument) { if (IsEnabled) { var tcs = new TaskCompletionSource <bool>(); Action action = () => dispatcher.Execute(async() => { await machine.FireAsync(parameterizedTrigger, argument); tcs.SetResult(true); if (!isInQueue) { RunFromQueueIfNotEmpty(); } }); if (isRunning || isPaused) { lock (actionsQueue) { actionsQueue = actionsQueue.Enqueue(action); queueCount++; } return(tcs.Task); } isRunning = true; action(); return(tcs.Task); } return(TaskCache.FalseTask); }
public void TestEnqueue() { int value = Generator.GetInt32(); var queue = ImmutableTreeQueue.Create <int>(); Assert.Empty(queue); // Enqueue doesn't change the original queue queue.Enqueue(value); Assert.Empty(queue); queue = queue.Enqueue(value); Assert.Single(queue); Assert.Equal(value, queue.Peek()); int[] expected = { value }; int[] actual = queue.ToArray(); Assert.Equal(expected, actual); // Test through the IImmutableQueue<T> interface IImmutableQueue <int> immutableQueue = queue; immutableQueue.Enqueue(Generator.GetInt32()); Assert.Equal(expected, immutableQueue); int nextValue = Generator.GetInt32(); immutableQueue = immutableQueue.Enqueue(nextValue); Assert.Equal(new[] { value, nextValue }, immutableQueue); }
/// <summary> /// Add to path. /// </summary> /// <returns></returns> private HierarchyBuilder ByAppName() { var next = new HierarchyBuilder(this); next._path = _path.Enqueue(APP_KEY); return(next); }
public override async Task MoveToStateAsync(TState state, StateTransitionOption option = StateTransitionOption.Default) { var flag = true; m_queueMonitor.Enter(); if (m_monitor.TryEnter()) { if (m_queueCount == 0) { m_queueMonitor.Exit(); flag = false; try { await base.MoveToStateAsync(state, option); } finally { m_queueMonitor.Enter(); if (m_queueCount > 0) { m_queueMonitor.Exit(); var _ = StartQueueIfNecessaryAsync(true); // Should not exit monitor here. Its handled by the process queue. } else { m_queueMonitor.Exit(); m_monitor.Exit(); } } } } if (flag) { var tcs = new TaskCompletionSource <bool>(); m_actionsQueue = m_actionsQueue.Enqueue(async() => { try { await base.MoveToStateAsync(state, option); tcs.TrySetResult(true); } catch (Exception ex) { tcs.SetException(ex); } }); m_queueCount++; m_queueMonitor.Exit(); var _ = StartQueueIfNecessaryAsync(); await tcs.Task; } }
public IImmutableQueue<TimeSpan> EnqueueElapsed(IImmutableQueue<TimeSpan> currentTimes) { if (!this.hasTimingInformation) return currentTimes; return currentTimes.Enqueue(this.Elapsed); }
public IImmutableQueue <TimeSpan> EnqueueElapsed(IImmutableQueue <TimeSpan> currentTimes) { if (!this.hasTimingInformation) { return(currentTimes); } return(currentTimes.Enqueue(this.Elapsed)); }
public void EnqueueDequeueTest() { this.EnqueueDequeueTestHelper(new GenericParameterHelper(1), new GenericParameterHelper(2), new GenericParameterHelper(3)); this.EnqueueDequeueTestHelper <GenericParameterHelper>(); // interface test IImmutableQueue <GenericParameterHelper> queueInterface = ImmutableQueue.Create <GenericParameterHelper>(); IImmutableQueue <GenericParameterHelper> populatedQueueInterface = queueInterface.Enqueue(new GenericParameterHelper(5)); Assert.Equal(new GenericParameterHelper(5), populatedQueueInterface.Peek()); }
public void Notify(T data) { lock (dequeueLocker) { buffer = buffer.Enqueue(data); var oldWaitable = waitable; waitable = new TaskCompletionSource <T>(); cancelToken.Register(() => waitable.TrySetCanceled()); oldWaitable.TrySetResult(data); } }
public static IImmutableQueue <T> enq <T>(IImmutableQueue <T> queue, T value) => queue.Enqueue(value);