1

Possible Duplicate:
Can constructors be async

I have a class Example that pulls several information from the internet on creation.

public class Example
{
    string information;    

    public Example()
    {
        //Pull information
    }
}

Now I would like to let Example become awaitable because it is important that Example is created before continuing the part after creation.

public async void SetupSomething()
{
    Example ex = new Example();
    await ex;
    // Do something with ex
}

How can I do this?

Community
  • 1
  • 1
TorbenJ
  • 4,462
  • 11
  • 47
  • 84
  • I'm voting for reopen because this question can actually be answered in it's original context while having nothing to do with async constructors. – Sinaesthetic May 17 '19 at 01:09
  • As svick suggested, I wouldn't call this a great approach, but honestly... I can't argue why other than it's a little odd and doesn't really read well. But you *can* actually do it. https://dotnetfiddle.net/lnsNQs – Sinaesthetic May 17 '19 at 01:18
  • There is a nice blogpost that answers the real question: https://devblogs.microsoft.com/pfxteam/await-anything/ – The Chris Sep 23 '20 at 06:43

2 Answers2

0

Well, the constructor should wait until it is done doing it's stuff. After all, the thread use to create it is from the method that calls it.

However, what I think would be very important, is to show us what that constructor is doing. There's a good chance it is calling something somewhere that start another thread and doesn't wait for it to resolve, making the constructor returns while not having all the "information" you are talking about.

Might be similar to: C# : Blocking a function call until condition met

Community
  • 1
  • 1
LightStriker
  • 19,738
  • 3
  • 23
  • 27
0

You could do that, but I think that's not a good approach, because there is nothing forcing the await before you start using the instance.

I think a good idea would be to create static asynchronous method that creates your object. Something like:

class Example
{
    private Example()
    {
    }

    private async Task InitializeAsync()
    {
        // get the required data asynchronously here
    }

    public static async Task<Example> CreateAsync()
    {
        var result = new Example();
        await result.InitializeAsync();
        return resul;
    }
}
svick
  • 236,525
  • 50
  • 385
  • 514
  • 1
    Only downvoting because it did not actually answer the question. – Sinaesthetic May 15 '19 at 23:56
  • @Sinaesthetic The question is how to create a class, when the construction involves `await`ing. Since constructor can't be `async`, you can use a factory method instead. How does that not answer the question? – svick May 16 '19 at 13:46
  • In the example given by the OP (`var ex = new Example(); await ex;`), instead you didn't answer how to create an awaitable class, you answered how to get around a constructor not being awaitable. This may not be your fault; perhaps the OP is poorly put. To create an awaitable class, you just have to have a GetAwaiter() method. – Sinaesthetic May 17 '19 at 00:57