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?

Redwood
  • 66,744
  • 41
  • 126
  • 187
  • 1
    You are running this as a Windows service, but trying to open a window? – Ron Beyer Jun 27 '19 at 18:44
  • 1
    Windows services cannot have any UI, at least it is not recommended, because the service has no desktop where to create the window. – RobertBaron Jun 27 '19 at 18:48
  • @RobertBaron, Ron Beyer we're certainly not intentionally opening a window, as you can see in the provided code that's not explicitly done anywhere. We're trying to use, e.g., the XpsDocument.GetFixedDocumentSequence() method to analyze an XPS document, but we don't even get that far. – Redwood Jun 27 '19 at 18:52
  • Is there some reason creating the dispatcher would also create a window? – Redwood Jun 27 '19 at 18:53
  • What Dispatcher is this? We cannot tell from the code that is posted. – RobertBaron Jun 27 '19 at 18:56
  • 1
    A functional dispatcher requires a window, it creates one if you haven't done so yourself. There is a hard upper-limit on the total number of windows that can be created by all processes that run on a desktop, 65535 in a session that interacts with the user. Much lower for session 0. A problem specific to only one machine are easy to deal with, you ask somebody else to throw it away or reformat its disk drive. – Hans Passant Jun 27 '19 at 19:00
  • @RobertBaron This is System.Windows.Threading.Dispatcher. – Redwood Jun 27 '19 at 19:03
  • @HansPassant Thanks for the info. It may be that we are indeed running up against the recommendation against having UI in Windows services just by using XpsDocument methods which requires a Dispatcher even though we're not actually displaying any UI to the user. If we determine that this is indeed the issue then if we moved the XPS logic to a command line executable and ran it from the Windows service using `System.Diagnostics.Process` would that avoid the issue or would it still run under session 0? (I'll do some research on this but maybe you know offhand.) – Redwood Jun 27 '19 at 19:10
  • See https://stackoverflow.com/questions/3351531/how-to-set-interact-with-desktop-in-windows-service-installer. This will provide desktop to the Window Service, but is not recommended by MS. – RobertBaron Jun 27 '19 at 19:20

0 Answers0