你可以设计围绕Task<T> class(任务并行库)的API:
class DistributedHashTable
{
public Task SetAsync(string key, string value);
public Task<string> GetAsync(string key);
}
这两种方法都将使用异步IO来执行相应的操作,并返回任务<牛逼>设置操作完成时已完成。
然后你可以同步使用你的类是这样的:
var dht = new DistributedHashTable(...);
dht.SetAsync("FOO", "bar").Wait();
Console.WriteLine(dht.GetAsync("FOO").Result);
或异步像这样:
var dht = new DistributedHashTable(...);
dht.SetAsync("FOO", "bar")
.ContinueWith(t => dht.GetAsync("FOO"))
.Unwrap()
.ContinueWith(t => {
Console.WriteLine(t.Result);
});
或异步使用Async CTP这样的:
var dht = new DistributedHashTable(...);
await dht.SetAsync("FOO", "bar");
Console.WriteLine(await dht.GetAsync("FOO"));
来源
2010-12-06 00:53:42
dtb
使用同步IO ? – ChaosPandion 2010-12-06 00:12:48
@ChaosPandion:我喜欢,但它是真的。 – 2010-12-06 00:13:55