Right,
you know the best solution for us, interesting.
This is exactly what we hat to avoid, multiple threads.
Don't tell us what the best solution is.
If you have a use case in your mind, regarding "BEST SOLUTION", do it for your implementation.
As stated before, the solution can be provided with a task scheduler,
I provided a task scheduler which is providing one thread as a sample.
It is not much code to exchange the thread in my samle against a message loop.
This is realy not the point.
And even with your statement that the code could end up with dead locks you are wrong.
Per se, dead locks cannot happen in single thread context.
In addition if you mean, that a dead lock can happen between the calling thread and this single tread,
than you are rigth, but it can also happen when using thread pool threads.
This has NOTHING to do with thread design
This has to do with locking, critical sections, mutexes etc.
If used wrong -> deadlock
And you are running in a dead lock, when the calling thread is doing a task.wait() AND the inner method is awaiting.
Than the task continuation cannot proceed, because the caller thread is blocke with the wait call.
But this is a wrong usage of wait and await and has NOTHING to do with single/multi threading.
No deadlock occur in our situation.
Instead of giving other people the best solution, just answer the question and do not provide your best solution you have in mind,
just because of you think thats the way to do.