title: Loader Lock Exception while playing audio using DirectX
body: <p>I am trying to play and audio file using DirectX dll.</p>
  
  <pre><code>  Audio.FromFile("Message 1.mp3").Play();
  </code></pre>
  
  <p>But a loader lock exception is detected as shown below:
  I am not an expert in analyzing the stack trace. How to analyze and solve the loader lock problem ?</p>
  
  <p><strong>LoaderLock was detected</strong></p>
  
  <pre><code>Message: DLL 'C:\Windows\assembly\GAC\Microsoft.DirectX\1.0.2902.0__31bf3856ad364e35\Microsoft.DirectX.dll' is attempting managed execution inside OS Loader lock. 
  Do not attempt to run managed code inside a DllMain or image initialization function since doing so can cause the application to hang.
  </code></pre>
  
  <p><strong>Stack Trace:</strong></p>
  
  <pre><code>'ToDoApp.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
  'ToDoApp.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities\9.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
  'ToDoApp.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\System.Windows.Forms\2.0.0.0__b77a5c561934e089\System.Windows.Forms.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
  'ToDoApp.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\System\2.0.0.0__b77a5c561934e089\System.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
  'ToDoApp.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\System.Drawing\2.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
  'ToDoApp.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities.Sync\9.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.Sync.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
  'ToDoApp.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.Debugger.Runtime\9.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.Debugger.Runtime.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
  'ToDoApp.vshost.exe' (Managed): Loaded 'E:\Modules\ToDoApp\bin\Release\ToDoApp.vshost.exe', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
  'ToDoApp.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC\Microsoft.DirectX.AudioVideoPlayback\1.0.2902.0__31bf3856ad364e35\Microsoft.DirectX.AudioVideoPlayback.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
  'ToDoApp.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\System.Core\3.5.0.0__b77a5c561934e089\System.Core.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
  'ToDoApp.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_32\System.Data\2.0.0.0__b77a5c561934e089\System.Data.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
  'ToDoApp.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\System.Data.DataSetExtensions\3.5.0.0__b77a5c561934e089\System.Data.DataSetExtensions.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
  'ToDoApp.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\System.Xml\2.0.0.0__b77a5c561934e089\System.Xml.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
  'ToDoApp.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\System.Xml.Linq\3.5.0.0__b77a5c561934e089\System.Xml.Linq.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
  The thread 0x127c has exited with code 0 (0x0).
  The thread 0x210 has exited with code 0 (0x0).
  'ToDoApp.vshost.exe' (Managed): Loaded 'E:\Modules\ToDoApp\bin\Release\ToDoApp.exe', Symbols loaded.
  The thread 0x15a8 has exited with code 0 (0x0).
  </code></pre>
  
  <p><strong>Edit:</strong>
  I tried disabling the loader lock exception. It makes the exception vanish, but still the file doesn't play.</p>
  
  <p>Any hints ?</p>