5

I'm trying to learn to write my own asynchronous methods, but I'm having difficulty, because ALL of the millions of examples that I have seen online ALL use await Task.Delay inside the custom async method and I neither want to add a delay into my code, nor have any other async method to call in its place.

Let's use a simple example, where I want to create a new collection of objects, with only two properties, from a huge existing collection of objects, that each have a great many properties. Let's say this is my synchronous code:

public List<SomeLightType> ToLightCollection(List<SomeType> collection)
{
    List<SomeLightType> lightCollection = new()
    foreach (SomeType item in collection)
    {
        lightCollection.Add(new SomeLightType(item.Id, item.Name));
    }
    return lightCollection;
}

To make this method asynchronous, do I just need to wrap it in a Task.Run, add the async keyword and suffix on the method name, and change the return type, as follows?:

public Task<List<SomeLightType>> ToLightCollectionAsync(List<SomeType> collection)
{
    List<SomeLightType> lightCollection = new()
    Task.Run(() =>
    {
        foreach (SomeType item in collection)
        {
            lightCollection.Add(new SomeLightType(item.Id, item.Name));
        }
    });
    return lightCollection;
}

Or do I also need to await the return of the Task inside the method? (The compiler gave me a warning until I added await.):

public async Task<List<SomeLightType>> ToLightCollectionAsync(List<SomeType> collection)
{
    List<SomeLightType> lightCollection = new()
    await Task.Run(() =>
    {
        foreach (SomeType item in collection)
        {
            lightCollection.Add(new SomeLightType(item.Id, item.Name));
        }
    });
    return lightCollection;
}

EDIT:

Oh yes, I have just realised that I need to await this operation, otherwise the empty collection will be returned before it is populated. But still, is this the correct way to make this code run asynchronously?

6
  • 7
    What is the purpose of wanting to make this code "async"? It's CPU bound work, there's no benefit per se to not just running it synchronously on the current thread, unless e.g. this is currently running on the UI thread. Aug 12, 2021 at 9:27
  • 3
    async and Task are mainly used when you have external resource dependencies like IO, Network, or database operations. Otherwise you should not use Task. Aug 12, 2021 at 9:30
  • 4
    Microsoft advises against writing asynchronous wrappers for synchronous methods: Should I expose asynchronous wrappers for synchronous methods? Aug 12, 2021 at 9:30
  • 3
    Errm... presumably he wants to run a long-running operation (creating a new collection) on a background thread using async/await to wait for the results? I'm not sure why everyone's asking why you might want to do that?
    – Rich N
    Aug 12, 2021 at 10:35
  • 1
    Thanks @RichN, that's exactly why I want this to be asynchronous. These collections contain many hundreds of thousands of items and there are a great many of these collections to convert. There is a method that calls this method for each of the collections and I definitely want that method to run asynchronously, as it currently takes several minutes.
    – WPF guy
    Aug 12, 2021 at 10:44

2 Answers 2

18

ALL of the millions of examples that I have seen online ALL use await Task.Delay inside the custom async method and I neither want to add a delay into my code, nor have any other async method to call in its place.

Task.Delay is commonly used as a "placeholder" meaning "replace this with your actual asynchronous work".

I'm trying to learn to write my own asynchronous methods

Asynchronous code begins at the "other end". The most common example is with an I/O operation: you can make this asynchronous instead of blocking the calling thread. At the lowest level, this is commonly done using a TaskCompletionSource<T>, which creates a Task<T> you can return immediately, and then later when the operation completes, you can use the TaskCompletionSource<T> to complete the Task<T>.

However, as you state in the comments:

I definitely want that method to run asynchronously, as it currently takes several minutes... this is a WPF application

What you really want is not asynchronous code; you want to run some code on a background thread so it doesn't block the UI thread. The code being run is CPU-bound and has no I/O to do, so it's just going to run on a thread pool thread instead of actually being asynchronous.

Let's use a simple example... To make this method asynchronous...

To run this code on a background thread, you would use Task.Run. However, I recommend that you do not implement this method using Task.Run. If you do, then you have a method that looks asynchronous but is not actually asynchronous; it's just running synchronously on a thread pool thread - what I call "fake asynchronous" (it has an asynchronous signature but is not actually asynchronous).

IMO, it's cleaner to keep your business logic synchronous, and in this case since you want to free up the UI thread, have the UI code call it using Task.Run:

// no change
public List<SomeLightType> ToLightCollection(List<SomeType> collection)
{
    List<SomeLightType> lightCollection = new()
    foreach (SomeType item in collection)
    {
        lightCollection.Add(new SomeLightType(item.Id, item.Name));
    }
    return lightCollection;
}

async void Button_Click(...)
{
  var source = ...
  var lights = await Task.Run(() => ToLightCollection(source));
  ... // Do something with lights
}
0
3

Task.Run is for CPU-Bound work (see learn.microsoft.com - Async in depth.

You can avoid await Task.Run() scenarios if you return the created task directly:

public Task<List<SomeLightType>> ToLightCollectionAsync(List<SomeType> collection) => Task.Run(() =>
{
    List<SomeLightType> lightCollection = new();
    // Do CPU bound work
    foreach (SomeType item in collection)
    {
        lightCollection.Add(new SomeLightType(item.Id, item.Name));
    }
    return lightCollection;
});

Now the caller can await for the result in an async Method to keep your UI responsive:

public async Task CallerMethod()
{
    // ...
    var result = await ToLightCollectionAsync(collection);
}

You also have the opportunity to perform some work during this computation.

public async Task CallerMethod()
{
    var task = ToLightCollectionAsync(collection);
    // Do some other work
    var result = await task;
}
4
  • 1
    Thank you for your helpful and non-judgemental answer. I changed my code to what you suggested and ran my code 10000 times, then averaged the time, to compare with my previous implementation. To be honest, there wasn't much difference, but I'm very glad to have learned more about this tricky subject, so thank you again.
    – WPF guy
    Aug 12, 2021 at 10:57
  • Why do you expect there to be a difference? Are you doing some other work while the task is running or simply awaiting it?
    – asaf92
    Aug 12, 2021 at 11:09
  • 2
    Concurrency and parallelism are different things. If you want do parallelize your computation you can split your source list into n pieces. If you change your input parameter of ToLightCollectionAsync to IEnumerable<T> you can use LINQ for this. Then you call this method for each piece without await (var task1 = ...; var task2 = .... At the end you can await for each started task to collect the results: var result1 = await task1; var result2 = await task2;. At the end you can merge your results with Concat if you need. (Using a shared output list needs locking and decreases performance).
    – Michael
    Aug 12, 2021 at 11:09
  • Task.Run() was the main purpose of doing async here. This won't block the UI thread but it will burden it.
    – ℍ ℍ
    Sep 2, 2021 at 21:19

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.