back

by danabramov·13y ago·view on hn ↗
There is some creative use of C# async/await in this blogpost:

http://praeclarum.org/post/45277337108/await-in-the-land-of-...

Basically, the author implements a “first time walkthrough” kind of interface a-la iWork very declaratively by using async:

    	async Task ShowTheUserHowToSearch ()
    	{
  		await Tutorial.EnterText (searchField, minLength: 3);
  		await Tutorial.Tap (searchButton);
  		await Tutorial.Congratulate ("Now you know how to search.");
  	}
1 comments
What if you have two buttons, search and 'be lucky'? I guess you will have to await for a controller object that wakes up that if either one of the buttons is triggered, and then test what button was pressed.

Now what if you have add a search option dialog that can be invoked at any moment? I guess when the 'apply' button of the option dialog got pressed then this is still going to be a callback that sets global variables according to search option.

So, like any mechanism, this way of structuring code has its limits. If an event can happen at any stage of the flow, then this still has to be handled by a callback, otherwise you would have to check for this kind of event after each await clause.

"What if you have two buttons, search and 'be lucky'? I guess you will have to await for a controller object that wakes up that if either one of the buttons is triggered, and then test what button was pressed."

http://msdn.microsoft.com/en-us/library/hh194796.aspx

... select or WaitForMultipleObjects in another form
In event driven programs there are often events that can arrive at any moment; for example in networking the connection might have been closed by the peer, or in a GUI the user might chose to alter parameters by means of option dialog.

So with await you may need to write a wrapper around await, the wrapper function will check for common events that can arrive at any moment.

Surely async isn't meant to replace events—it's just in some cases, when you expect events to happen in particular order, such as in help tutorial, async gives an advantage.