public void StartAsyncLoad(string screenType, Action afterLoaded = null) { if (AsyncLoadingState == AsyncLoadingState.LoadingScreen) { #if DEBUG throw new InvalidOperationException("This Screen is already loading a Screen of type " + asyncScreenTypeToLoad + ". This is a DEBUG-only exception"); #endif } else if (AsyncLoadingState == AsyncLoadingState.Done) { #if DEBUG throw new InvalidOperationException("This Screen has already loaded a Screen of type " + asyncScreenTypeToLoad + ". This is a DEBUG-only exception"); #endif } else { AsyncLoadingState = AsyncLoadingState.LoadingScreen; asyncScreenTypeToLoad = ScreenManager.MainAssembly.GetType(screenType); if (asyncScreenTypeToLoad == null) { throw new Exception("Could not find the type " + screenType); } #if REQUIRES_PRIMARY_THREAD_LOADING // We're going to do the "async" loading on the primary thread // since we can't actually do it async. PerformAsyncLoad(); if (afterLoaded != null) { afterLoaded(); } #else mNumberOfThreadsBeforeAsync = FlatRedBallServices.GetNumberOfThreadsToUse(); FlatRedBallServices.SetNumberOfThreadsToUse(1); Action action; if (afterLoaded == null) { action = (Action)PerformAsyncLoad; } else { action = () => { PerformAsyncLoad(); // We're going to add this to the instruction manager so it executes on the main thread: InstructionManager.AddSafe(new DelegateInstruction(afterLoaded)); }; } #if WINDOWS_8 || UWP System.Threading.Tasks.Task.Run(action); #else ThreadStart threadStart = new ThreadStart(action); Thread thread = new Thread(threadStart); thread.Start(); #endif #endif } }