I'm designing a silverlight game and I'm stuck with the game end functions. It's a turn based card game and I'm using WCF Duplex. But when I call finalize function, callbacks are receiving OnEndGame message before the OnFinalized message. I need that to play some animations in client before starting a new game. I tried many things inlcuding Thread.Sleep and Task.Wait etc. but I couldn't figure out the problem. What should I do to give some time for EndGame method to return?
private void FinalizeGame()
{
this.GameState = Enums.GameState.Finalize;
Task.Factory.StartNew(()=>{
CalculateWinners();
GameSubscriptionManager.Publish(SubscriptionType.GameStream, cb => cb.OnFinalize(this));
}).ContinueWith((antecedent)=>{
EndGame();
});
}
private void EndGame()
{
this.GameState = Enums.GameState.None;
Thread.Sleep(5000);
GameSubscriptionManager.Publish(cb => cb.OnEndGame(this));
RemovePlayersAndGetWaitingPlayers();
if (PlayingPlayers.Count > 1)
{
ResetGame();
StartGame();
}
else
{
GameState = GameState.None;
GameSubscriptionManager.Publish(cb => cb.OnWaitingForNewPlayers(this));
}
}