What must I do to make my methods awaitable?
Your method
private async Task TestAsyncAwait()
{
int i = await TaSLs_Classes.TASLsUtils.GetZoomSettingForDistance(5);
}
should be written like this
private async Task TestAsyncAwait()
{
Task<int> t = new Task<int>(() =>
{
return TaSLs_Classes.TASLsUtils.GetZoomSettingForDistance(5);
});
t.Start();
await t;
}
If you need to return the int, replace the Task type:
private async Task<int> TestAsyncAwait()
{
Task<int> t = new Task<int>(() =>
{
return TaSLs_Classes.TASLsUtils.GetZoomSettingForDistance(5);
});
t.Start();
return await t;
}
You only need to return an awaitable. Task
/Task<TResult>
is a common choice; Task
s can be created using Task.Run
(to execute code on a background thread) or TaskCompletionSource<T>
(to wrap an asynchronous event).
Read the Task-Based Asynchronous Pattern for more information.