简体   繁体   中英

WPF .NET3.5 C# Handle Object Issue

I have some trouble with my App, WPF C#.NET 3.5 VS2008, at one customer machine. My App has nearly 1000 Installation on different windows machines (XP, Vista 32/64, Win7 32/64) and no problem so far.

Only one seems to be have a Problem on a Win7 32 PC.

Task-Manger Overview depending on the current Activity:

1 - Activity: Program started

http://i.stack.imgur.com/jA6j7.gif (See Pic)

2 - Activity: Simple click on a eg Menu Item, Button etc

http://i.stack.imgur.com/e0erH.gif (See Pic)

Object USER and Object GDI runs until 10'000 and the App throws the following Exception:

System.ComponentModel.Win32Exception The current process has used all of its system allowance of handles for Window Manager objects 

à 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)
   à MS.Win32.HwndWrapper..ctor(Int32 classStyle, Int32 style, Int32 exStyle, Int32 x, Int32 y, Int32 width, Int32 height, String name, IntPtr parent, HwndWrapperHook[] hooks)
   à System.Windows.Interop.HwndSource.Initialize(HwndSourceParameters parameters)
   à System.Windows.Interop.HwndSource..ctor(HwndSourceParameters parameters)
   à System.Windows.Window.SetTaskbarStatus()
   à System.Windows.Window.CreateAllStyle()
   à System.Windows.Window.CreateSourceWindowImpl()
   à System.Windows.Window.SafeCreateWindow()
   à System.Windows.Window.ShowHelper(Object booleanBox)
   à System.Windows.Window.Show()
   à System.Windows.Window.ShowDialog()
   à WPFControls.WPFMessageBox.Show(String title, String message, String details, WPFMessageBoxButtons buttonOption, WPFMessageBoxImage image)
   à MyApp.Prolog.Application_DispatcherUnhandledException(Object sender, DispatcherUnhandledExceptionEventArgs e)
   à System.Windows.Threading.Dispatcher.CatchException(Exception e)
   à System.Windows.Threading.Dispatcher.CatchExceptionStatic(Object source, Exception e)
   à System.Windows.Threading.ExceptionWrapper.CatchException(Object source, Exception e, Delegate catchHandler)
   à System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
   à System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
   à System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Boolean isSingleParameter)
   à System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority, Delegate method, Object arg)
   à MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   à MS.Win32.UnsafeNativeMethods.SetWindowPos(HandleRef hWnd, HandleRef hWndInsertAfter, Int32 x, Int32 y, Int32 cx, Int32 cy, Int32 flags)
   à System.Windows.Window.SetupInitialState(Double requestedTop, Double requestedLeft, Double requestedWidth, Double requestedHeight)
   à System.Windows.Window.CreateSourceWindowImpl()
   à System.Windows.Window.SafeCreateWindow()
   à System.Windows.Window.ShowHelper(Object booleanBox)
   à System.Windows.Window.Show()
   à System.Windows.Window.ShowDialog()
   à WPFControls.WPFMessageBox.Show(String title, String message, String details, WPFMessageBoxButtons buttonOption, WPFMessageBoxImage image)
   à MyApp.Prolog.Application_DispatcherUnhandledException(Object sender, DispatcherUnhandledExceptionEventArgs e)
   à System.Windows.Threading.Dispatcher.CatchException(Exception e)
   à System.Windows.Threading.Dispatcher.CatchExceptionStatic(Object source, Exception e)
   à System.Windows.Threading.ExceptionWrapper.CatchException(Object source, Exception e, Delegate catchHandler)
   à System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
   à System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
   à System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Boolean isSingleParameter)
   à System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority, Delegate method, Object arg)
   à MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   à MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
   à System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
   à System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
   à System.Windows.Window.ShowHelper(Object booleanBox)
   à System.Windows.Window.Show()
   à System.Windows.Window.ShowDialog()
   à MyApp.Prolog.Prolog_Startup(Object sender, StartupEventArgs e) 0 0 0

Please can anyone help ?


UPDATE - EDIT

Our Customer does Setup a new clean (as he said) Win 7 64 OS, but the Problem still exist. Using WPF Apps seeems to be not working.

I have tried also to use the MVVM Demo App of this Article (msdn.microsoft.com/de-de/magazine/dd419663.aspx) by John Smith at the Customers Machine.

The MVVM Demo App crashes also, when i try i click to a Menu-Item, Button or select something in a DropDown-List. Enter a Text in a Textfield is okay, but click and select does stop the MVVM Demo App with the Error that the Application does not work.

Here a Event-Log of my App

<Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
    - <System>
        <Provider Name="Application Error" />
        <EventID Qualifiers="0">1000</EventID>
        <Level>2</Level>
        <Task>100</Task>
        <Keywords>0x80000000000000</Keywords>
        <TimeCreated SystemTime="2013-02-07T11:04:13.000000000Z" />
        <EventRecordID>2243</EventRecordID>
        <Channel>Application</Channel>
        <Computer>RSSec.Machine.local</Computer>
        <Security />
    </System>
    - <EventData>
        <Data>MyApp.exe</Data>
        <Data>2.0.16.0</Data>
        <Data>51136bd8</Data>
        <Data>unknown</Data>
        <Data>0.0.0.0</Data>
        <Data>00000000</Data>
        <Data>c000041d</Data>
        <Data>74494f0d</Data>
        <Data>ecc</Data>
        <Data>01ce0522d9d3bc67</Data>
        <Data>C:\Program Files (x86)\MyApp\MyApp.exe</Data>
        <Data>unknown</Data>
        <Data>1ad6d91f-7116-11e2-87c7-10604b689bfe</Data>
    </EventData>
</Event>

Can anyone help again ? Thx

There is at least one problem in your code that's evident from the stack trace. You've written an event handler for the Dispatcher.UnhandledException event and display the exception with a message box. What you do not do is make sure that you avoid doing this if this message box is already displayed. Visible by the multiple appearances of MyApp.Prolog.Application_DispatcherUnhandledException() in the trace.

So there are some odds here that the user is looking at the screen filled with ten thousand message boxes. After which Windows pulled the plug when it refused to allow your program to create any more.

You do not yet know what the real exception looks like. But it is likely to be something gross, something that throws over and over again when the dispatcher loop runs. You'll need to improve your unhandled exception handling to get the real reason it crashed. Do add a bool flag that indicates that the message box is shown and terminate the program when UnhandledException fires again with the flag set to true. And log the exceptions so you can find out what was thrown without the user having to read back what the message box said.

Every windows platform has some handle limit. older ones had sometimes even as low as ~16000. older ones even consumed 1 handle per 1 control (text box, button) on the screen. older ones sometimes even did not check for the limit what presented curious behaviors when sometimes only a half of your window opened up.. But anywhere, everywhere, it always was one of two things: your application was too handle-greedy simply because of wrong UI design (just try displaying in WinForms a repeater with no virtualization with 100'000 rows and a button/textedit inside each of them!), or your application leaked handles by NOT-FREEing the UI/IO/etc items properly. Or you tried opening million of files at once, but that's obvious and much less probable.

Anyways, the exception says handles-to-window-manager, so it's rather all about UI.

Probably the fastest way to diagnoze which handles are leaking is to fetch a program like ProcessExplorer from MSDN or ProcessHacker2 from SourceForge, and try inspecting what is going on just before the crash.

Above-mentioned applications allow you to view not only what handles are currently open by any application (here you learn about pending I/Os, Mutexes, etc), but also you can inspect the current Window hierarchy (there you will maybe learn what windows/controls have not been freed, or which ones were unnecessarily created in multiples). I say maybe, because it's very application-specific. In some cases with Proceshacker you will se exactly that your "Add comment" window is opened in 10000 instances, but in other cases you will just lear that 10000 handles to "a window" are hanging, what doesn't tell you much.

Of course, that's just beginning. Then you'll have to find and fix the cause. But at least that's some start and you will know where to look further.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM