/// <summary>
        /// Enqueues the command for execution.
        /// </summary>
        /// <param name="command">The command.</param>
        private void EnqueueCommand(MediaCommand command)
        {
            if (MediaCore.IsOpen == false)
            {
                command.Complete();
                return;
            }

            lock (SyncLock)
                Commands.Add(command);
        }
Example #2
0
        /// <summary>
        /// Closes the specified media.
        /// This command gets processed in a threadpool thread asynchronously.
        /// </summary>
        /// <returns>Returns the background task.</returns>
        public async Task CloseAsync()
        {
            if (CanExecuteCommands == false)
            {
                return;
            }
            else
            {
                IsClosing.Value = true;
            }

            var command = new CloseCommand(this);

            ExecutingCommand = command;
            ClearCommandQueue();

            var action = new Action(() =>
            {
                try
                {
                    if (command.HasCompleted)
                    {
                        return;
                    }
                    command.RunSynchronously();
                }
                catch (Exception ex)
                {
                    MediaCore?.Log(
                        MediaLogMessageType.Error,
                        $"{nameof(MediaCommandManager)}.{nameof(CloseAsync)}: {ex.GetType()} - {ex.Message}");
                }
                finally
                {
                    ExecutingCommand?.Complete();
                    ExecutingCommand = null;
                    IsClosing.Value  = false;
                }
            });

            await Task.Run(action);
        }