我正在创建一个包含一系列事件的类,其中一个是GameShuttingDown.触发此事件时,我需要调用事件处理程序.此事件的目的是通知用户游戏正在关闭,他们需要保存他们的数据.保存是等待的,事件不是.因此,当处理程序被调用时,游戏会在等待处理程序完成之前关闭.
public event EventHandler<EventArgs> GameShuttingDown;
public virtual async Task ShutdownGame()
{
await this.NotifyGameShuttingDown();
await this.SaveWorlds();
this.NotifyGameShutDown();
}
private async Task SaveWorlds()
{
foreach (DefaultWorld world in this.Worlds)
{
await this.worldService.SaveWorld(world);
}
}
protected virtual void NotifyGameShuttingDown()
{
var handler = this.GameShuttingDown;
if (handler == null)
{
return;
}
handler(this, new EventArgs());
}
Run Code Online (Sandbox Code Playgroud)
// The game gets shut down before this completes because of the nature of how events work
DefaultGame.GameShuttingDown += async (sender, args) => await this.repo.Save(blah); …Run Code Online (Sandbox Code Playgroud) 我使用Microsoft Visual Studio Ultimate 2013,看起来这个按钮在一个项目(WPF应用程序)中工作,而不是在同一个解决方案中的其他项目(服务器应用程序)中工作.哪个VS设置我可能已经坏了?