public static void ExecuteCoroutineThread(CoroutineHandle handle) { try { while (!handle.AbortRequested) { if (handle.Coroutine.Current != null) { WaitForSeconds wfs = handle.Coroutine.Current as WaitForSeconds; if (wfs != null) { Thread.Sleep((int)(wfs.TotalTime * 1000)); } else { switch ((CoroutineStatus)handle.Coroutine.Current) { case CoroutineStatus.Success: return; case CoroutineStatus.Failure: DebugConsole.ThrowError("Coroutine \"" + handle.Name + "\" has failed"); return; } } } Thread.Yield(); if (!handle.Coroutine.MoveNext()) { return; } } } catch (ThreadAbortException) { //not an error, don't worry about it } catch (Exception e) { handle.Exception = e; DebugConsole.ThrowError("Coroutine \"" + handle.Name + "\" has thrown an exception", e); } }
public CameraTransition(ISpatialEntity targetEntity, Camera cam, Alignment?cameraStartPos, Alignment?cameraEndPos, bool fadeOut = true, float duration = 10.0f, float?startZoom = null, float?endZoom = null) { Duration = duration; FadeOut = fadeOut; this.cameraStartPos = cameraStartPos; this.cameraEndPos = cameraEndPos; this.startZoom = startZoom; this.endZoom = endZoom; AssignedCamera = cam; if (targetEntity == null) { return; } Running = true; CoroutineManager.StopCoroutines("CameraTransition"); updateCoroutine = CoroutineManager.StartCoroutine(Update(targetEntity, cam), "CameraTransition"); }
/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { GraphicsWidth = GraphicsDevice.Viewport.Width; GraphicsHeight = GraphicsDevice.Viewport.Height; ConvertUnits.SetDisplayUnitToSimUnitRatio(Physics.DisplayToSimRation); spriteBatch = new SpriteBatch(GraphicsDevice); TextureLoader.Init(GraphicsDevice); loadingScreenOpen = true; TitleScreen = new LoadingScreen(GraphicsDevice); loadingCoroutine = CoroutineManager.StartCoroutine(Load()); var myForm = (System.Windows.Forms.Form)System.Windows.Forms.Form.FromHandle(Window.Handle); myForm.Deactivate += new EventHandler(HandleDefocus); myForm.Activated += new EventHandler(HandleFocus); }
/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { GraphicsWidth = GraphicsDevice.Viewport.Width; GraphicsHeight = GraphicsDevice.Viewport.Height; ConvertUnits.SetDisplayUnitToSimUnitRatio(Physics.DisplayToSimRation); spriteBatch = new SpriteBatch(GraphicsDevice); TextureLoader.Init(GraphicsDevice); loadingScreenOpen = true; TitleScreen = new LoadingScreen(GraphicsDevice); bool canLoadInSeparateThread = false; #if WINDOWS canLoadInSeparateThread = true; #endif loadingCoroutine = CoroutineManager.StartCoroutine(Load(), "", canLoadInSeparateThread); }
public static CoroutineHandle StartCoroutine(IEnumerable <object> func, string name = "", bool useSeparateThread = false) { var handle = new CoroutineHandle(func.GetEnumerator(), name); lock (Coroutines) { Coroutines.Add(handle); } handle.Thread = null; if (useSeparateThread) { handle.Thread = new Thread(() => { ExecuteCoroutineThread(handle); }) { Name = "Coroutine Thread (" + handle.Name + ")", IsBackground = true }; handle.Thread.Start(); } return(handle); }
private static bool IsDone(CoroutineHandle handle) { try { if (handle.Coroutine.Current != null) { WaitForSeconds wfs = handle.Coroutine.Current as WaitForSeconds; if (wfs != null) { if (!wfs.CheckFinished(UnscaledDeltaTime)) { return(false); } } else { switch ((CoroutineStatus)handle.Coroutine.Current) { case CoroutineStatus.Success: return(true); case CoroutineStatus.Failure: DebugConsole.ThrowError("Coroutine \"" + handle.Name + "\" has failed"); return(true); } } } handle.Coroutine.MoveNext(); return(false); } catch (Exception e) { DebugConsole.ThrowError("Coroutine " + handle.Name + " threw an exception: " + e.Message + "\n" + e.StackTrace.ToString()); return(true); } }
public static void StopCoroutines(CoroutineHandle handle) { Coroutines.RemoveAll(c => c == handle); }
public static bool IsCoroutineRunning(CoroutineHandle handle) { return(Coroutines.Contains(handle)); }
private static bool IsDone(CoroutineHandle handle) { #if !DEBUG try { #endif if (handle.Thread == null) { if (handle.AbortRequested) { return(true); } if (handle.Coroutine.Current != null) { WaitForSeconds wfs = handle.Coroutine.Current as WaitForSeconds; if (wfs != null) { if (!wfs.CheckFinished(UnscaledDeltaTime)) { return(false); } } else { switch ((CoroutineStatus)handle.Coroutine.Current) { case CoroutineStatus.Success: return(true); case CoroutineStatus.Failure: DebugConsole.ThrowError("Coroutine \"" + handle.Name + "\" has failed"); return(true); } } } handle.Coroutine.MoveNext(); return(false); } else { if (handle.Thread.ThreadState.HasFlag(ThreadState.Stopped)) { if (handle.Exception != null || (CoroutineStatus)handle.Coroutine.Current == CoroutineStatus.Failure) { DebugConsole.ThrowError("Coroutine \"" + handle.Name + "\" has failed"); } return(true); } return(false); } #if !DEBUG } catch (Exception e) { #if CLIENT && WINDOWS if (e is SharpDX.SharpDXException) { throw; } #endif DebugConsole.ThrowError("Coroutine " + handle.Name + " threw an exception: " + e.Message + "\n" + e.StackTrace.ToString()); handle.Exception = e; return(true); } #endif }