Geowigo.Models.WFCoreAdapter.WaitForGameState C# (CSharp) Method

WaitForGameState() private method

private WaitForGameState ( EngineGameState target ) : void
target EngineGameState
return void
		private void WaitForGameState(EngineGameState target)
		{
			// Immediately returns if the engine is in the target state,
			// or if the engine has previously crashed.
			lock (_SyncRoot)
			{
				if (_IsInCrash)
				{
					return;
				}
			}
			try
			{
				CheckStateIs(target, null);
				
				// If we get here, it means that the engine is in the target state.
				return;
			}
			catch (InvalidOperationException)
			{
				// The engine is performing a concurrent operation.
				// Let's keep on going.
			}

			// Sets up a manual reset event that is set when the engine state
			// changes to the target game state.
			System.Threading.ManualResetEvent resetEvent = new System.Threading.ManualResetEvent(false);
			PropertyChangedEventHandler handler = new PropertyChangedEventHandler((o, e) =>
			{
				if (e.PropertyName == "GameState")
				{
					try
					{
						CheckStateIs(target, null);

						// The engine is not in a concurrent game operation.
						// Let's signal the event.
						resetEvent.Set();
					}
					catch (InvalidOperationException)
					{
						// The engine is performing a concurrent operation.
						// Let's wait some more.
						return;
					}
				}
			});
			PropertyChanged += handler;

			// Waits on the event.
			resetEvent.WaitOne();

			// Removes the handler.
			PropertyChanged -= handler;
		}
		#endregion