In following code snippet, I am accessing async method in sync context. The parent thread is running in thread pool.
public async Task AsyncWriteToEventHub(Data data, string correlationId)
{
await this.eventHubClinet.SendBatchAsync(data);
}
public void WriteData(Data data)
{
AsyncWriteToEventHub(Data);
}
Surprisingly, I am not getting any compilation error while calling AsyncWriteToEventHub()
event though I am not using await()
or wait()
in WriteData
.
Questions:
- Will there be a deadlock since I am running these functions in thread pool?
- Does
AsyncWriteToEventHub
will run on separate thread? If Yes, I am not waiting on the thread object? - What will happen to child thread when parent completes the execution?
AsyncWriteToEventHub
is a fire-and-forget method for me.
CodePudding user response:
Will there be a deadlock since I am running these functions in thread pool?
There is no deadlock. Your code is not blocking on asynchronous code, so the classic deadlock (as described on my blog) will not occur.
Does AsyncWriteToEventHub will run on separate thread?
No. Why would it?
If Yes, I am not waiting on the thread object? What will happen when parent completes the execution?
Your code is not (a)waiting for the method to complete. So your code never knows when the method completes. So your code cannot know when it is safe for the application to exit, and it cannot even be aware of exceptions thrown by the asynchronous method.
Usually, this is not desirable. It's more natural to await
every task sooner or later.