2

编辑:不,上面链接的帖子中没有回答这个问题。

如果我有这个伪代码:

// do some stuff first
wait until ClassName.EventName fires without blocking
// do the rest of the code

有没有办法在 C# 中使用 .NET 4.0 做到这一点?

编辑:

我已经下载了 Async Targetting Pack,我正在尝试用它来做这件事。(我的项目针对 .NET 4.0。)

我现在添加了这样的代码。

private static async void WaitAsyncNoBlock()
{
    await dynMapServLayer.IsInitialized;                 
}

所以它被称为这样。

// do some stuff first
WaitAsyncNoBlock();
// do some more stuff

但我收到错误消息“无法等待 Bool”。

4

2 回答 2

4

If you are talking about using await in your code like you can in .NET 4.5 you can use the Async Targeting Pack from Microsoft to add that functionality to your .NET 4.0 programs.

于 2013-05-21T19:03:50.913 回答
0

您必须创建一个句柄来注释事件。而且,当它触发时,你运行你的代码。

例如:

MyForm.FormClosed += new FormClosedEventHandler(MyForm_FormClosed);

和:

void MyForm_FormClosed(object sender, FormClosedEventArgs e){
//Here you put the rest of code
}
于 2013-05-21T19:07:22.983 回答