private void shutdownEngine() { Trace.WriteLine("Shutting down engine and forcing garbage collection.", "notice"); try{ if (engine != null) { engine.Dispose(); engine = null; } //If there are any bugs involving destructors or disposing unmanaged objects, hopefully this will trigger them. GC.Collect(); } catch (Exception ex) { Trace.WriteLine("Engine shutdown failed: " + ex.ToString(), "warning"); } finally{ engine = null; } }
private void switchState(FractalEngineState rs) { FractalEngineState cs = this.EngineState; //the current engine state try{ if (rs == cs) { return; //they match, do nothing } else if (cs == FractalEngineState.Online) { if (rs == FractalEngineState.Suspended) //go from online to suspended { engine.Deallocate(); } else if (rs == FractalEngineState.Offline) //go from online to offline { shutdownEngine(); } } else if (cs == FractalEngineState.Suspended) { if (rs == FractalEngineState.Online) //go from suspended to online { allocateEngine(); } else if (rs == FractalEngineState.Offline) //go from suspended to offline { shutdownEngine(); } } else if (cs == FractalEngineState.Offline) { if (rs == FractalEngineState.Online) //go from offline to online { deviceEntry = chooseDevice(); engine = deviceEntry.CreateFractalEngine(mainForm.Renderer.Context); allocateEngine(); } else if (rs == FractalEngineState.Suspended) //go from offline to suspended { deviceEntry = chooseDevice(); engine = deviceEntry.CreateFractalEngine(mainForm.Renderer.Context); } } else if (cs == FractalEngineState.Error) { if (rs == FractalEngineState.Offline) //go from error to offline { shutdownEngine(); } } } catch (Exception ex) //if something goes wrong, switch to the error state { shutdownEngine(); if (ex is NoDevicesFoundException) { currentException = ex; } else { currentException = new FractalEngineStateException(deviceEntry == null ? EngineType.Auto : deviceEntry.EngineType, cs, rs, ex); } } }