title: Why does LongRunning task (TPL) with JpegBitmapDecoder run out of resources?
body: <p>We have a managed .Net / C# application that creates TPL tasks to perform JPEG metadata encoding on JPEG images. Each task is constructed with TaskCreationOptions.LongRunning option, e.g., </p>
  
  <pre><code>Task task = new Task( () =&gt; TaskProc(), cancelToken, TaskCreationOptions.LongRunning );
  </code></pre>
  
  <p>TaskProc() utilizes JpegBitmapDecoder and JpegBitmapEncoder classes to add JPEG metadata and save new images to disk.
  We allow up to 2 such tasks to be active at any one time, and this process should continue indefinitely.</p>
  
  <p>After some time of performing the aforementioned we get <em>Not enough storage is available
  to process this command</em> exception when trying to create an instance of JpegBitmapDecoder class:</p>
  
  <blockquote>
    <p>System.ComponentModel.Win32Exception (0x80004005): Not enough storage
    is available to process this command      at
    MS.Win32.UnsafeNativeMethods.RegisterClassEx(WNDCLASSEX_D wc_d)<br>
    at MS.Win32.HwndWrapper..ctor(Int32 classStyle, Int32 style, Int32
    exStyle, Int3 2 x, Int32 y, Int32 width, Int32 height, String name,
    IntPtr parent, HwndWrapperHoo k[] hooks)      at
    System.Windows.Threading.Dispatcher..ctor()      at
    System.Windows.Threading.Dispatcher.get_CurrentDispatcher()      at
    System.Windows.Media.Imaging.BitmapDecoder..ctor(Stream bitmapStream,
    BitmapC reateOptions createOptions, BitmapCacheOption cacheOption,
    Guid expectedClsId)      at
    System.Windows.Media.Imaging.JpegBitmapDecoder..ctor(Stream
    bitmapStream, Bit mapCreateOptions createOptions, BitmapCacheOption
    cacheOption)</p>
  </blockquote>
  
  <p>The error occurred <b>only</b> when we utilized JpegBitmapDecoder to add metadata. In other words, if the task would just encode &amp; save a Bitmap image to file, no problems arose. Nothing obvious was revealed when using Process Explorer, Process Monitor, or other diagnostics tools. No thread, memory, or handle leaks were observed at all. When such error occurs, no new applications can be launched, e.g., notepad, word, etc.
  Once our application is terminated, everything goes back to normal.</p>
  
  <p>The task creation option of LongRunning is defined in MSDN as <em>Specifies that a task will be a long-running, coarse-grained operation. It provides a hint to the TaskScheduler that oversubscription may be warranted.</em> This implies that the thread chosen to run the task may not be from the ThreadPool, i.e., it will be created for the purpose of the task. The other task creation options will result in a ThreadPool thread being selected for the task.</p>
  
  <p>After some time analyzing and testing, we changed the task creation option to anything other than <em>LongRunning</em>, e.g., <em>PreferFairness</em>. No other changes to the code were made at all. This "resolved" the problem, i.e., no more running out of storage errors.</p>
  
  <p>We are puzzled as to the actual reason for LongRunning threads being the culprit. 
  Here are some of our questions on this:</p>
  
  <ol>
  <li><p>Why should the fact that the threads chosen to execute the task come from the ThreadPool or not? If the thread terminates, shouldn't its resources be reclaimed over time by GC and returned back to the OS, regardless of its origin?</p></li>
  <li><p>What is so special about the combination of a LongRunning task and JpegBitmapDecoder's functionality that causes the error?</p></li>
  </ol>