Here is the simple example:
public event EventHandler CookinDone = delegate{};
public void CoockinRequest(){
var indicator = new ActivityIndicator();
ActivityIndicator.Show("Oooo coockin' something cool");
var bw = new BackgroundWorker();
bw.DoWork += (sender, e) => CockinService.Cook();
bw.RunWorkerCompleted += (sender, e) => {
indicator.Hide();
CookinDone.Invoke(this,null);
};
bw.RunWorkerAsync();
}
Now, everytime I use that method I have to intercept CookinDone event and move on.
var cook = new Cook();
cook.CookinDone += (sender, e) => MessageBox.Show("Yay, smells good");
cook.CoockinRequest();
But how can I simplify that by making return type of the method as Boolean and return result upon the Cookin completion?
if (CoockinRequest()) MessageBox.Show('Yay, smells even better');
if I put there something like while (bw.IsBusy)
it will screw my ActivityIndicator, freeze the main thread and I feel it would be the lousiest thing to do. There are also some Monitor.Wait
stuff and some other stuff like TaskFactory
, but all that stuff seems to be too complicated to use in simple scenarios.
It might be also different in different environments, like some approach is good for WPF apps, some for something else and whatnot, but there should be a general pattern isn't that right?
How do you do that guys?
There isn't a direct way to do this in .NET 4. This is actually very much in line with the new async/await functionality coming in the next release of C#.
The Task Parallel Library can be used in .NET 4 to accomplish this today. You would do this by changing your code to return a Task<bool>
, so the caller could wait on it (if required), or subscribe a continuation on the task which would run when this was complete.
To do this, you'd rewrite the above code like so:
public Task<bool> CoockinRequestAsync()
{
var indicator = new ActivityIndicator();
ActivityIndicator.Show("Oooo coockin' something cool");
// This assumes Cook() returns bool...
var task = Task.Factory.StartNew(CockinService.Cook);
// Handle your removal of the indicator here....
task.ContinueWith( (t) =>
{
indicator.Hide();
}, TaskScheduler.FromCurrentSynchronizationContext());
// Return the task so the caller can schedule their own completions
return task;
}
Then, when you go to use this, you'd write something like:
private void SomeMethod()
{
var request = this.CoockinRequestAsync();
request.ContinueWith( t =>
{
// This will run when the request completes...
bool result = t.Result;
// Use result as needed here, ie: update your UI
}, TaskScheduler.FromCurrentSynchronizationContext());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With