1

I have a question regardin the async method which I call in constructor and how to solve or is there a good work around, here is an example

public Constructor()
{
 Value = PopulateValueFromDB(); //async method
 CalculateInDB(); // async method
}

public async Task<string> PopulateValueFromDB()
{
 ... do some async calls
 return await ...
}

public async Task CalculateInDB()
{
 ...
 return await ...
}

Basically in constructor i have an error, because i cannot use await there, and i cannot make it async.

For CalculateInDB i can make return it void, then i solve the issue with it, although i read somewhere that returning void is not very good solution.

Regarding the PopulateVlaue method ...i have to return something ...

So is there a work around ir i shouldn't use those methods then and make them sync instead of async?

Alnedru
  • 2,573
  • 9
  • 50
  • 88
  • 1
    Why do you want this? What are you trying to do? What do you expect to happen if you call a property that has not been initialized yet? – Ortiga Jan 25 '14 at 18:36
  • Hmm, i'm just trying to convert all my methods to being async :) it is a web application ... yeah ... while it reads data from database the resource is free for another request – Alnedru Jan 25 '14 at 18:49
  • and ofcourse it is not the idea to have a property not initialized :/ – Alnedru Jan 25 '14 at 18:49
  • 1
    Please, don't do this. Async opens new threads, which comes with a cost. Use async to parallelize slow computations, to wait for a external resource to respond, and this sort of things. Using async for fast methods actually makes them slower. Plus, in a web application, every request already runs in they own thread. Opening several more threads can fill the thread pool, making your code wait threads to be freed, and making everything slower. – Ortiga Jan 25 '14 at 19:08
  • [Patterns of Parallel Programming](http://www.microsoft.com/en-us/download/details.aspx?id=19222) is a good resource if you want to learn more about parallelization. – Ortiga Jan 25 '14 at 19:09
  • 3
    @Andre `async` *does not* "open new thread"... `async`/`await` really have not much to do with threads (unless you know that particular operation does create threads, but most regular ones like web/IO requests don't). Note that it is not new concept either - check out [Asynchronous vs Threading](http://social.msdn.microsoft.com/Forums/vstudio/en-US/fa9e1830-ef06-4dd1-8ef7-59ebf04ab1e6/asynchronous-vs-threading?forum=netfxbcl) posts from 2006 – Alexei Levenkov Jan 25 '14 at 19:43
  • If i undestand it correctly in web context, async/await, thread doing something else while for example a database opreation is executed in async mode, so for example it can server other requests for example etc ... – Alnedru Jan 26 '14 at 11:00

2 Answers2

6

I have a blog post on async constructors that covers a variety of approaches. If possible, I recommend you use the factory pattern, as such:

private Constructor()
{
}

private async Task InitializeAsync()
{
  Value = await PopulateValueFromDBAsync();
  await CalculateInDBAsync();
}

public static async Task<Constructor> Create()
{
  var ret = new Constructor();
  await ret.InitializeAsync();
  return ret;
}
Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
-4

This is a time to use old tech!

ThreadPool.QueueUserWorkItem.

Cheers -

simon at rcl
  • 7,326
  • 1
  • 17
  • 24