I want to create some async Tasks without starting them at the moment. With the untyped Tasks there is no problem:
private Task CreateTask() {
return new Task(
async () => await DoSomething().ConfigureAwait(false));
}
But to handle Exceptions I added a return value to my function. But how can I write it in a correct syntax:
private Task<bool> CreateTask() {
return new Task<bool>(
async () => await DoSomething().ConfigureAwait(false));
}
I get the message, that the async lambda expression can't be converted to func. So my question: How is it written correctly?
DoSomething()already returns a running Task so you can just return it as-is,return (Task)DoSomething();.awaitawaits for an already running task to complete, it doesn't make it start asynchronously. There's no reason to create a cold task withnew. In the second case, what is the value supposed to be?awaitcall with an exception handler, just as you would with a synchronous method.awaitwill cause the original exception to be thrown. You could write egtry{ .... await CallMyServerAsync();}catch(WebException exc){...}just as you would writetry{ .... await CallMyServer();}catch(WebException exc){...}ifCallMyServeris a blocking method