4

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?

3
  • 1
    Both methods are wrong. DoSomething() already returns a running Task so you can just return it as-is, return (Task)DoSomething();. await awaits for an already running task to complete, it doesn't make it start asynchronously. There's no reason to create a cold task with new. In the second case, what is the value supposed to be? Commented Dec 9, 2016 at 13:04
  • 1
    This sounds like a case of the XY Problem. You have a problem with X, think that Y is the solution and ask about Y when it doesn't work. What is the actual problem you want to solve? Commented Dec 9, 2016 at 13:05
  • To handle exceptions enclose your await call with an exception handler, just as you would with a synchronous method. await will cause the original exception to be thrown. You could write eg try{ .... await CallMyServerAsync();}catch(WebException exc){...} just as you would write try{ .... await CallMyServer();}catch(WebException exc){...} if CallMyServer is a blocking method Commented Dec 9, 2016 at 13:12

1 Answer 1

3

I want to create some async Tasks without starting them at the moment.

That is the wrong solution for whatever problem you're trying to solve. If you want to define code that you want to run in the future, then you should use a delegate:

private Func<Task> CreateTaskFactory()
{
  return async () => await DoSomething().ConfigureAwait(false);
}

But to handle Exceptions I added a return value to my function.

Again, that's not the best solution. Tasks already understand exceptions just fine without writing any additional code.

If you do need to return a value (as data), then you can return a delegate that creates a Task<T>:

private Func<Task<int>> CreateTaskFactory()
{
  return async () => await DoSomethingReturningInt().ConfigureAwait(false);
}
Sign up to request clarification or add additional context in comments.

5 Comments

CreateTaskFactory can just be return DoSomething(); There's no advantage to an async delegate here.
@Servy: I'm assuming that the op wants to define the operation now, but start it at a later time.
Sorry, meant return () => DoSomething();
@Servy: Oh, yeah. Absolutely, unless the op's delegates are not trivial.
Sure, although if it's non-trivial enough it would most likely best be refactored out into a named method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.