Is it possible to await an event instead of another async method?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
In my C#/XAML metro app, there's a button which kicks off a long-running process. So, as recommended, I'm using async/await to make sure the UI thread doesn't get blocked:
private async void Button_Click_1(object sender, RoutedEventArgs e) { await GetResults(); } private async Task GetResults() { // Do lot of complex stuff that takes a long time // (e.g. contact some web services) ... }
Occasionally, the stuff happening within GetResults would require additional user input before it can continue. For simplicity, let's say the user just has to click a "continue" button.
My question is: how can I suspend the execution of GetResults in such a way that it awaits an event such as the click of another button?
Here's an ugly way to achieve what I'm looking for: the event handler for the continue" button sets a flag...
buttonContinue.Visibility = Visibility.Visible; while (!_continue) await Task.Delay(100); // poll _continue every 100ms buttonContinue.Visibility = Visibility.Collapsed;
The polling is clearly terrible (busy waiting / waste of cycles) and I'm looking for something event-based.
Any ideas?
Btw in this simplified example, one solution would be of course to split up GetResults() into two parts, invoke the first part from the start button and the second part from the continue button. In reality, the stuff happening in GetResults is more complex and different types of user input can be required at different points within the execution. So breaking up the logic into multiple methods would be non-trivial.
private SemaphoreSlim signal = new SemaphoreSlim(0, 1); // set signal in event signal.Release(); // wait for signal somewhere else await signal.WaitAsync();
Alternatively, you can use an instance of the TaskCompletionSource Class to create a Task that represents the result of the button click:
private TaskCompletionSource tcs = new TaskCompletionSource(); // complete task in event tcs.SetResult(true); // wait for task somewhere else await tcs.Task;
回答2:
When you have an unusual thing you need to await on, the easiest answer is often TaskCompletionSource (or some async-enabled primitive based on TaskCompletionSource).
In this case, your need is quite simple, so you can just use TaskCompletionSource directly:
private TaskCompletionSource
Logically, TaskCompletionSource is like an asyncManualResetEvent, except that you can only "set" the event once and the event can have a "result" (in this case, we're not using it, so we just set the result to null).
回答3:
Ideally, you don't. While you certainly can block the async thread, that's a waste of resources, and not ideal.
Consider the canonical example where the user goes to lunch while the button is waiting to be clicked.
If you have halted your asynchronous code while waiting for the input from the user, then it's just wasting resources while that thread is paused.
That said, it's better if in your asynchronous operation, you set the state that you need to maintain to the point where the button is enabled and you're "waiting" on a click. At that point, your GetResults method stops.
Then, when the button is clicked, based on the state that you have stored, you start another asynchronous task to continue the work.
Because the SynchronizationContext will be captured in the event handler that calls GetResults (the compiler will do this as a result of using the await keyword being used, and the fact that SynchronizationContext.Current should be non-null, given you are in a UI application), you can use async/await like so:
private async void Button_Click_1(object sender, RoutedEventArgs e) { await GetResults(); // Show dialog/UI element. This code has been marshaled // back to the UI thread because the SynchronizationContext // was captured behind the scenes when // await was called on the previous line. ... // Check continue, if true, then continue with another async task. if (_continue) await ContinueToGetResultsAsync(); } private bool _continue = false; private void buttonContinue_Click(object sender, RoutedEventArgs e) { _continue = true; } private async Task GetResults() { // Do lot of complex stuff that takes a long time // (e.g. contact some web services) ... }
ContinueToGetResultsAsync is the method that continues to get the results in the event that your button is pushed. If your button is not pushed, then your event handler does nothing.
回答4:
Here is a utility class that I use:
public class AsyncEventListener { private readonly Func _predicate; public AsyncEventListener() : this(() => true) { } public AsyncEventListener(Func predicate) { _predicate = predicate; Successfully = new Task(() => { }); } public void Listen(object sender, EventArgs eventArgs) { if (!Successfully.IsCompleted && _predicate.Invoke()) { Successfully.RunSynchronously(); } } public Task Successfully { get; } }
And here is how I use it:
var itChanged = new AsyncEventListener(); someObject.PropertyChanged += itChanged.Listen; // ... make it change ... await itChanged.Successfully; someObject.PropertyChanged -= itChanged.Listen;
回答5:
Stephen Toub published this AsyncManualResetEvent class on his blog.
public class AsyncManualResetEvent { private volatile TaskCompletionSource m_tcs = new TaskCompletionSource(); public Task WaitAsync() { return m_tcs.Task; } public void Set() { var tcs = m_tcs; Task.Factory.StartNew(s => ((TaskCompletionSource)s).TrySetResult(true), tcs, CancellationToken.None, TaskCreationOptions.PreferFairness, TaskScheduler.Default); tcs.Task.Wait(); } public void Reset() { while (true) { var tcs = m_tcs; if (!tcs.Task.IsCompleted || Interlocked.CompareExchange(ref m_tcs, new TaskCompletionSource(), tcs) == tcs) return; } } }
回答6:
Simple Helper Class:
public class EventAwaiter { #region Fields private TaskCompletionSource _eventArrived = new TaskCompletionSource(); #endregion Fields #region Properties public Task Task { get; set; } public EventHandler Subscription => (s, e) => _eventArrived.TrySetResult(e); #endregion Properties }
Usage:
var valueChangedEventAwaiter = new EventAwaiter(); example.YourEvent += valueChangedEventAwaiter.Subscription; await valueChangedEventAwaiter.Task;