【发布时间】:2013-03-10 07:35:56
【问题描述】:
我正在包装一个库供我自己使用。要获得某个属性,我需要等待一个事件。我正在尝试将其包装到异步调用中。
基本上我想转
void Prepare()
{
foo = new Foo();
foo.Initialized += OnFooInit;
foo.Start();
}
string Bar
{
return foo.Bar; // Only available after OnFooInit has been called.
}
进入这个
async string GetBarAsync()
{
foo = new Foo();
foo.Initialized += OnFooInit;
foo.Start();
// Wait for OnFooInit to be called and run, but don't know how
return foo.Bar;
}
如何才能最好地做到这一点?我可以循环等待,但我正在尝试找到更好的方法,例如使用 Monitor.Pulse()、AutoResetEvent 或其他方法。
【问题讨论】:
标签: c# asynchronous async-await