public async Task <Response> Request(Request request) { // Send request var resultReady = new AutoResetEvent(false); RequestQueue[request.Id] = resultReady; await RpcLayer.SendRequest(request); await Task.Run(() => resultReady.WaitOne()); // Remove lock from queue AutoResetEvent removedEvent; RequestQueue.TryRemove(request.Id, out removedEvent); // Retrieve result var result = ResultCache[request.Id]; Response dequeuedResponse; ResultCache.TryRemove(request.Id, out dequeuedResponse); return(result); }