I have a question regarding async await pattern. I have following code
internal async Task<int> ConsumedBytesAsync(byte[] buffer)
{
while(condition)
{
//build message
if (message != null)
{
if (_previousRegisterMessageTask != null)
{
await _previousRegisterMessageTask;
}
_previousRegisterMessageTask = _controllers.RegisterMessageAsync(message, chunk.MessageHeader.MessageStreamId);
}
}
return totalConsumed;
}
And the code in other class.
internal async Task RegisterMessageAsync(IMessage message, uint messageid)
{
// some stuff about retrieve resposible controller
if (controller != null)
{
await controller.HandleMessage(message, action);
}
}
I would like to increase performance by run RegisterMessage asynchronously and in the same time start build next message. When next message is built procecss should await for finish registration of previous message. In ideal world it should works fine but actually _controllers.RegisterMessageAsync method execute synchronously.
I've checked that by Stopwatch start and stop around that method (I assumed that elapsed time should be around 0 - 5 becuase of only operation which should be done is return and save Task but was 800-900 what is average execution time of this method) and Control.WriteLine within _controllers.RegisterMessageAsync after await and after save Task to _previousRegisterMessageTask (Control.WriteLine from _controllers.RegisterMessageAsync appear firstly.)
So please let me know why that operation works synchronously instead of asynchronously and what can i do to achieve logic I descriebe above. Maybe I did wrong tests and it is works asynchronously ? Thanks in advance.