First, some clarifications
A method doesn't need an await to be async
, it's the other way around. You can only use await
in an async
method. You may have a method that returns a Task
without it being marked as async
. More here.
Your actual question
IIUC, is about "The Root Of All Async".
So, yes, it is possible to create a "first" async
method. It's simply a method that returns a Task
(or Task<T>
).
There are two types of these tasks: A Delegate Task and a Promise Task.
- A Delegate Task is fired by
Task.Run
(most times. Task.StartNew
and new Task(() => {});
are other options) and it has code to run. This is mostly used for offloading work.
- A Promise Task is a
Task
that doesn't actually execute any code, it's only a mechanism to a. Wait to be notified upon completion by the Task
. b. Signaling completion using the Task
. This is done with TaskCompletionSource
and is mostly used for inherently asynchronous operations (but also for async
synchronization objects) for example:
.
private static Task DoAsync()
{
var tcs = new TaskCompletionSource<int>();
new Thread(() =>
{
Thread.Sleep(1000);
tcs.SetResult(5);
}).Start();
return tcs.Task;
}
These tools allow you to create those "roots", but unless you implement an I/O
library (like .Net
's DNS
class) or a synchronization object (like SemaphoreSlim
) yourself, you would rarely use them.