title: What are the reasons for SEHException in multi threaded DirectX code?
body: <p>I am working with a fairly large and complicated codebase I am not familiar with, which uses <strong>DirectWrite</strong> for text rendering. I don't know whether if it matters, but this is a WinRT (Windows Store) application.</p>
  
  <p>The app is <strong>multi threaded</strong>, and some DirectX calls are done from a background (not UI) thread. All the <strong>BeginDraw</strong> and <strong>EndDraw</strong> calls are made from the <strong>UI thread</strong>, and some of the <strong>DrawText</strong> calls are made from <strong>background</strong> threads. I made sure that none of these calls overlap, and DrawText is only called between BeginDraw and EndDraw pairs.</p>
  
  <p>However, sometimes I get a <strong>SEHException</strong> when calling the DrawText method. It never happens when it is called from the <strong>UI thread</strong>, only when called from a <strong>background</strong> thread.<br>
  This does not happen on every computer. It very often happens on some, and can not be reproduced at all on others.<br>
  The exact message the VS Debugger says is this: "A first chance exception of type 'System.Runtime.InteropServices.SEHException' occurred in x.DLL", and it happens when I call the <strong>ID2D1DeviceContext.DrawText()</strong> function, and the debugger breaks in the d2d1.h header file at the DrawText call.</p>
  
  <p>Is there any way to debug or diagnose what is going wrong when a SEHException is thrown? (It is clearly connected to calling the DrawText function from a background thread.)<br>
  And apart from this, are there any general rules of calling DirectX functions from background threads? (I made sure that the D2D1Factory gets created with D2D1_FACTORY_TYPE_MULTI_THREADED option.)</p>