Beispiel #1
0
        public Task <string> SendAsync(int request)
        {
            var tcs  = new TaskCompletionSource <string>();
            var item = new CorrelationItem <int>(request);

            _map.TryAdd(item.Correlation, tcs);
            RequestChannel.TryAdd(item);
            return(tcs.Task);
        }
Beispiel #2
0
 private void WatchLoop()
 {
     foreach (var r in RequestChannel.GetConsumingEnumerable())
     {
         var request = r;                    // capture variable
         Thread.Sleep(request.Value * 1000); // BAD PRACTICE (should be await Task.Delay)
         var item = new CorrelationItem <string>(request.Correlation, $"Data of {request.Value}");
         ResponseChannel.TryAdd(item);
     }
 }
Beispiel #3
0
 private void WatchLoopAsync()
 {
     foreach (var r in RequestChannel.GetConsumingEnumerable())
     {
         var request = r; // capture variable
         var t       = Task.Run(async() =>
         {
             await Task.Delay(request.Value * 1000).ConfigureAwait(false);
             var item = new CorrelationItem <string>(request.Correlation, $"Data of {request.Value}");
             ResponseChannel.TryAdd(item);
         });
         t.Start();
     }
 }
Beispiel #4
0
 private void WatchLoop()
 {
     foreach (var r in RequestChannel.GetConsumingEnumerable())
     {
         var request = r; // capture variable
         var t       = new Thread(() =>
         {
             Thread.Sleep(request.Value * 1000); // BAD PRACTICE
             var item = new CorrelationItem <string>(request.Correlation, $"Data of {request.Value}");
             ResponseChannel.TryAdd(item);
         });
         t.Start();
     }
 }
Beispiel #5
0
        static void Main(string[] args)
        {
            _server = new Server();

            //TODO: remove the Dequeue loop
            Thread t = new Thread(DequeueLoop);

            t.Start();

            int[] requets = { 1, 5, 1, 3 };
            foreach (var r in requets)
            {
                var item = new CorrelationItem <int>(r);
                Console.WriteLine($"Sending: {item.Value} [{item.Correlation:N}]");
                RequestChannel.Add(item);
                // TODO: replace _requestChannel.Add(item); with wrapper that return Task<string>
            }

            Console.ReadKey();
        }