Current thread dispatcher creation fails on new STA thread

0

I have a Windows service running under the Local System account that analyses XPS documents.

On one machine creating the dispatcher on the analysis thread always fails, but on all the other machines we've tried it has worked without any problems.

The failing machine is running Server 2012 R2 with .NET 4.7.2, I believe we've previously tested this code on that OS and everything worked but I'm setting up a VM to confirm that now.

Here's a minimal version of the code that's failing:

var staThread = new Thread(() => {
    var _ = Dispatcher.CurrentDispatcher;

    // Code that does the analysis would be here but is never reached
});

staThread.SetApartmentState(ApartmentState.STA);
staThread.Start();
staThread.Join();

And here's the exception:

System.ComponentModel.Win32Exception (0x80004005): Not enough storage is available to process this command
   at MS.Win32.UnsafeNativeMethods.CreateWindowEx(Int32 dwExStyle, String lpszClassName, String lpszWindowName, Int32 style, Int32 x, Int32 y, Int32 width, Int32 height, HandleRef hWndParent, HandleRef hMenu, HandleRef hInst, Object pvParam)
   at MS.Win32.HwndWrapper..ctor(Int32 classStyle, Int32 style, Int32 exStyle, Int32 x, Int32 y, Int32 width, Int32 height, String name, IntPtr parent, HwndWrapperHook[] hooks)
   at System.Windows.Threading.Dispatcher..ctor()
   at System.Windows.Threading.Dispatcher.get_CurrentDispatcher()
   ... [name of the class/method with the above code omitted]

I've seen threads like this one talking about memory leaks causing this issue, but it's interesting that this issue occurs every time this code is run on this machine and all we're doing is trying to create the dispatcher, so if that's the issue it's not a long term leak so much as the limits getting exceeded all at once somehow.

As recommended in that thread I did try looking at handles, user objects, and GDI objects in task Manager while it was running and handles were around 800, user objects and GDI objects were both 0 before the code ran and then it crashed immediately (so if any of those values changed it was too quickly to see before the crash).

Any ideas?

c#
dispatcher
sta
asked on Stack Overflow Jun 27, 2019 by Lawrence Johnston • edited Jun 27, 2019 by Lawrence Johnston

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0