同步方法 调用异步防范,在同步方法异步调用

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, 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 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());

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值