Beispiel #1
0
        /// <summary>
        /// Execute a command and waits for a response (Unsafe because of Task Cancelation)
        /// </summary>
        /// <param name="method"></param>
        /// <param name="id"></param>
        /// <param name="parameters"></param>
        /// <exception cref="TaskCanceledException"></exception>
        /// <returns></returns>
        private Task <CommandResult <T> > UnsafeExecuteCommandWithResponse <T>(METHODS method, int id = 0, List <object> parameters = null)
        {
            CommandResultHandler <T> commandResultHandler;

            lock (_currentCommandResults)
            {
                if (_currentCommandResults.TryGetValue(id, out ICommandResultHandler oldHandler))
                {
                    oldHandler.TrySetCanceled();
                    _currentCommandResults.Remove(id);
                }

                commandResultHandler = new CommandResultHandler <T>();
                _currentCommandResults.Add(id, commandResultHandler);
            }

            ExecuteCommand(method, id, parameters);

            return(commandResultHandler.Task);
        }
Beispiel #2
0
        /// <summary>
        /// Execute a command and waits for a response (Unsafe because of Task Cancelation)
        /// </summary>
        /// <param name="method"></param>
        /// <param name="id"></param>
        /// <param name="parameters"></param>
        /// <exception cref="TaskCanceledException"></exception>
        /// <returns></returns>
        private async Task <CommandResult <T> > UnsafeExecuteCommandWithResponse <T>(METHODS method, int id = 0, List <object> parameters = null)
        {
            CommandResultHandler <T> commandResultHandler;

            lock (_currentCommandResults)
            {
                if (_currentCommandResults.TryGetValue(id, out ICommandResultHandler oldHandler))
                {
                    oldHandler.TrySetCanceled();
                    _currentCommandResults.Remove(id);
                }

                commandResultHandler = new CommandResultHandler <T>();
                _currentCommandResults.Add(id, commandResultHandler);
            }

            try
            {
                ExecuteCommand(method, id, parameters);
                return(await commandResultHandler.Task);
            }
            finally
            {
                lock (_currentCommandResults)
                {
                    // remove the command if its the current handler in the dictionary
                    if (_currentCommandResults.TryGetValue(id, out ICommandResultHandler currentHandler))
                    {
                        if (commandResultHandler == currentHandler)
                        {
                            _currentCommandResults.Remove(id);
                        }
                    }
                }
            }
        }