Havret edited a comment on pull request #59:
URL: https://github.com/apache/activemq-nms-amqp/pull/59#issuecomment-780898935


   @brudo Please take a look at the following snippet:
   
   ```
   class Program
   {
       private static object syncRoot = new object();
   
       static async Task Main(string[] args)
       {
           lock (syncRoot)
           {
               Console.WriteLine($"Before not awaited on thread 
{Thread.CurrentThread.ManagedThreadId}.");
               NotAwaitedMethod();
               Console.WriteLine($"After not awaited on thread 
{Thread.CurrentThread.ManagedThreadId}.");
           }
   
           await Task.Delay(TimeSpan.FromSeconds(2));
       }
   
       private static async Task NotAwaitedMethod()
       {
           Console.WriteLine($"Inside before await on thread 
{Thread.CurrentThread.ManagedThreadId}.");
           await Task.Delay(TimeSpan.FromSeconds(1));
           Console.WriteLine($"Inside after await on thread 
{Thread.CurrentThread.ManagedThreadId}.");
       }
   }
   ```
   
   The output will be as follows: 
   ```
   Before not awaited on thread 1.
   Inside before await on thread 1.
   After not awaited on thread 1.
   Inside after await on thread 4.
   ```
   
   In order to preserve the async-await behavior, you need to push it all the 
way up the call stack. If you do that and at the same time try to fool the 
compiler by changing the lock statement to Monitor.Enter, Monitor.Exit we would 
in fact end up with a deadlock. 


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to