Example #1
0
        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 MoveToState(TState state, StateTransitionOption option = StateTransitionOption.Default)
 {
     return(!IsEnabled
         ? Task.FromResult(false)
         : RunOnScheduler(() => machine.MoveToStateInternal(state, option)));
 }