4

I've grasped the concept of async await and have been using it sporadically, but do have a couple questions regarding best practices.

  1. is it ok to use await in a while(condition) loop to keep fetching data that may be present, until the while condition changes, e.g. stopProcessingMessages = false.

  2. in an application such as winforms, while UI runs on it's thread, using async/await on an operation such as a button click is fairly trivial, but what about if I would like to enforce asynchronously throughout an entire console application, or even a windows service. what is the best practice to initially kick off that first await task, would that be Task.Run (() => ... )?

I hope I am making sense in my 2nd question. I want to make the most of async and utilize it to it's full extent, but just need to understand how to kick off the initial asynchronous operation before it bubbles down to all other asynchronous functions.

apologies for not using the proper code blocks I am on the train using my smartphone.

3 답변


7

I've grasped the concept of async await and have been using it sporadically, but do have a couple questions regarding best practices.

I have an intro to async/await blog post that goes into more detail than most intros and also introduces several best practices.

is it ok to use await in a while(condition) loop to keep fetching data that may be present, until the while condition changes, e.g. stopProcessingMessages = false.

You want to avoid tight loops. So the while (condition) GetDataIfPresent(); is going to consume a lot of CPU.

Alternatively, you could use an async method that returned null (or whatever) if stopProcessingMessages is true. In this case, your code would be while (true), and a more TAP-like solution would be to use CancellationSource instead of a flag.

Also take a look at TPL Dataflow; it sounds like it may be useful for your kind of situation.

console application, or even a windows service. what is the best practice to initially kick off that first await task

For console apps, you could Wait on the top-level task. This is an acceptable exception to the usual guideline (which is to await instead of Wait). Waiting will burn a thread for the duration of the console app, but that's usually not important enough to warrant a more complex solution. If you do want to install a single-threaded context for your console app, you could use AsyncContext.Run from my AsyncEx library.

For Win32 services, you usually do need to start your own thread. You can use Task.Run for this (if you want a multithreaded context), or AsyncContextThread from AsyncEx (if you want a single-threaded context).


  • thank you for the concise answer Stephen. I actually ended up going with TPL Dataflow's BufferBlock. in a console environment I also found that Task.Run (async () => await .... ); to kick of an initial asynchronous process will also suffice. - Sash
  • Re the console top-level async action: Task.Run wouldn't gain you anything over simply calling an async method. You still need to Wait on the returned task to wait for it to complete before exiting. - Stephen Cleary

2

Good morning,

I would rather use a regular task with the TaskCreationOption set to 'LongRunning' in your first scenario than the async/await pattern. This way your whole while block would be executed in one long running task. When using await inside each while loop you would start a new task with every loop - would work, but it's maybe not so optimal ;-)

Regarding your second question, I'm sorry but I don't get your point.

Hope this helps.


  • Indeed, a loop full of awaits will actually start... how many new tasks? Unless that stopProcessingMessages variable gets set in a hurry, it has the potential to just spin up thousands of threads. That said, what does "keep fetching" even mean in the question context? A single task has one result, not a stream of data to be continually fetched. - Snixtor

0

It is not ok to use a loop to keep fething data that may be present.. You can create an async call that upon completion will automaticlly invoke a callback method.. the "waiting" phase in that case will happen in the OS mechanisms which treat this waiting phase in optimum way to the OS being used.

Take a look here for further study of the subject: http://msdn.microsoft.com/en-us/library/vstudio/hh191443.aspx

Linked


Related

Latest