Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asynchronous call in synchronous method

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?

like image 753
iLemming Avatar asked Oct 20 '11 21:10

iLemming


1 Answers

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());
}
like image 184
Reed Copsey Avatar answered Oct 20 '22 15:10

Reed Copsey