In a console application I use async-await as follows:
static void Main(string[] args)
{
// Task<Model>
var taskGetModel = Testcases.GetModel(22);
taskGetModel.Wait();
// Task without TResult
var taskSaveModel = Testcases.SaveModel(taskGetModel.Result);
taskSaveModel.Wait();
}
public async static Task<Model> GetModel(int number)
{
var baseData = await serviceagent.GetBaseData();
var model = await serviceagent.GetModel(baseData, number);
model.BaseData = baseData;
return model;
}
public static async Task SaveModel(Model model)
{
await serviceagent.SaveModel(model);
}
// as for the service methods:
public async Task SaveModel(Model model)
{
// the method `.ToCommunication` has the signature:
// public async Task<CommunicationModel> ToCommunication
var commodel = await new Mapper().ToCommunication(model);
// Proxy is the interface to the servce (ChannelFactory etc.)
// the method `.Save` has the signature:
// public async Task Save(CommunicationModel model)
await Proxy.Save(commodel);
}
public async Task<Model> GetModel(BaseData baseData, int number)
{
// NOT a task: CommunicationModel GetCommunicationModel
var commodel = Proxy.GetCommunicationModel(baseData, number);
// signature with Task: public async Task<Model> ToModel
return await new Mapper().ToModel(baseData, commodel);
}
In static main, the Task<TResult> gives the nice result that the function GetModel returns immediately with the Task and I can wait for its result to finish.
Why does the Task in SaveModel not return immediately?
The await in SaveModel is already awaited in that method.
Is it because it doesn not have a TResult?
SaveModelmethod do? Is it genuinely async? It would really help if you would provide a short but complete program demonstrating the problem. Note that writing an async method whose onlyawaitis the very last thing is rarely useful - it's just wrapping one async (presumably) operation in another. - Jon SkeetTaskvsTask<TResult>. It is most likely a difference between howSaveModelandGetBaseDataare implemented. - Marc GravellTaskorTask<T>(or another awaitable type), it doesn't guarantee that it actually is asynchronous in its entirety (or at all, it could return aTaskwith anIsCompletedstate oftrue). Without seeing whatProxy.Savedoes,ToCommunicationdoes or whatMapper()is, it's impossible to tell for sure. - Peter Ritchie