Creating a typed async task

I want to create several async tasks without starting them at the moment. There are no problems with untyped tasks:

    private Task CreateTask() {
        return new Task(
             async () => await DoSomething().ConfigureAwait(false));
    }

But for exception handling, I added a return value for my function. But how can I write it in the correct syntax:

    private Task<bool> CreateTask() {
        return new Task<bool>(
            async () =>  await DoSomething().ConfigureAwait(false));
    }

I get a message that an asynchronous lambda expression cannot be converted to func. So my question is: how is it spelled correctly?

+4
source share
1 answer

I want to create several asynchronous tasks without starting them at the moment.

, . , , :

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

.

, . , .

( ), , Task<T>:

private Func<Task<int>> CreateTaskFactory()
{
  return async () => await DoSomethingReturningInt().ConfigureAwait(false);
}
+3

Source: https://habr.com/ru/post/1663311/


All Articles