As @Dev has suggested, This is not how you use async await.
you need to declare an asynchronous method first. And then use await keyword to wait for the result or call to finish.
private async void PostToServer(Model Data)
{
//Do complex task without returning anything.
//Like posting data to server.
//...Do something with model
await context.SaveAsync();
}
async Task<ObjPrice> getprice()
{
//Do complex task and return result.
//Like waiting for some other event to update the price.
return await WaitForPriceChangeAsync();
}
public async Task Bar()
{
var objStockcheck = await WaitForPriceUpdate();
await PostToServer();
}
Read more about async-await here:
https://learn.microsoft.com/en-us/dotnet/csharp/async
How and When to use `async` and `await`
Edit:
Creating asynchronous task
private async Task<string> RandomTaskAsync()
{
var result=await Task.Run<String>(=>
{
//Doing any task here will run in asynchronously
return HugeComputing();
});
return result;
}
This method can be awaited in other method calls.
Also, As suggested by Jon Hanna, use async void only for event handlers (as there is no alternative in that case). Using otherwise violates the TAP principle.