   [.net] Exiting properly
  Ok, I have my gameloop that looks something like this...// Start rendering_dx.Renderer.beginScene();// End rendering_dx.Renderer.endScene();Application.DoEvents();Now, in endScene() I have device.Present(). When I run my program, and click the exit button on the window, the window closes and it gives me an exception at the device.Present() line. I do have try {}catch(DirectXException){} in there, but is there something I should change to make it work properly?Microsoft.DirectX.DirectXException was unhandled  Message="Error in the application."  Source="Microsoft.DirectX"  AdditionalInformation=""  ErrorCode=-2147467259  ErrorString="E_FAIL"  StackTrace:  at Microsoft.DirectX.Direct3D.Device.Present(tagRECT* pSource, tagRECT* pDest, HWND__* hOverride)  at Microsoft.DirectX.Direct3D.Device.Present()  at SoC.Core.Renderer.cRenderer.endScene() in C:\Documents and Settings\ryan\Desktop\soc\SoC\Core\Renderer\cRenderer.cs:line 42  at SoC.cKernel.gameLoop() in C:\Documents and Settings\ryan\Desktop\soc\SoC\cKernel.cs:line 58  at SoC.cKernel..ctor() in C:\Documents and Settings\ryan\Desktop\soc\SoC\cKernel.cs:line 36  at SoC.main.Main(String[] args) in C:\Documents and Settings\ryan\Desktop\soc\SoC\main.cs:line 18  at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)  at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)  at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()  at System.Threading.ThreadHelper.ThreadStart_Context(Object state)  at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)  at System.Threading.ThreadHelper.ThreadStart()
  When (which statement must be true) do you exit the gameloop and what does the code looks like that is executed when the form closes? Also, what does your Present() call look like?   ;  Its when I exit the form frmMain_OnClose, which calls Application.Exit(). I guess I could send a message to the GameState manager telling it to stop the game loop. I'll need to try that...My endScene() looks likedevice.EndScene()device.Present();I added a try/catch statement in there to stop it from whining when I closed the form.   ;  Are you disposing/implementing the disposable   pattern correctly?I am thinking that maybe the finalizer messes things up. Btw, you should Dispose() all objects before you Application.Exit().   ;  try onClosing instead of onClose. at the time on close is called, some resources may already be disposed. the other reason for using onClosing is that you can cancel the closing of your form (by setting the eventargs' cancel property to tue)   ;  How does you main game loop look like? I use the Paint() off my form to do all the rendering, so when the application is exited, it stops drawing, and thus it's safe to dispose the MDX object.If you're not doing it like that, you might want to use some form of flagging to make sure you're not continueing to draw when you're closing down. For instance, if you're implementing the IDisposable interface(Which I encourage), you can call:if (!_dx.IsDisposed){ // Yes, the object is valid, let's render!    // Start rendering    _dx.Renderer.beginScene();    // End rendering    _dx.Renderer.endScene();    Application.DoEvents();}This way, you know for sure when to render adn when not. And in the else, you could exit the for loop...Toolmaker   