Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
611 views
in Technique[技术] by (71.8m points)

c# - Win32Exception: Operation completed successfully

The following code crashes with an exception :

MyWindow wnd = new MyWindow();
wnd.Show(); //here an exception occurs

Exception is rather strange but as I understand its a bug in .net

System.ComponentModel.Win32Exception (0x80004005): The operation completed successfully
   at MS.Win32.UnsafeNativeMethods.GetDC(HandleRef hWnd)
   at System.Windows.Interop.HwndTarget..ctor(IntPtr hwnd)
   at System.Windows.Interop.HwndSource.Initialize(HwndSourceParameters parameters)
   at System.Windows.Interop.HwndSource..ctor(HwndSourceParameters parameters)
   at System.Windows.Window.CreateSourceWindow(Boolean duringShow)
   at System.Windows.Window.CreateSourceWindowDuringShow()
   at System.Windows.Window.SafeCreateWindowDuringShow()
   at System.Windows.Window.ShowHelper(Object booleanBox)
   at System.Windows.Window.Show()

MyWindow object is a window with some vector graphics inside, but not too much. Also, it happens when 10-20 MyWindow objects have been opened and closed already.

Solution: The reason was a leak of GDI objects.They were creating in my low level code containing a mistake. So, the problem had no concern to MyWindow object.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It doesn't bomb on a winapi error code, the actual error code is E_FAIL, a COM error code. Which is very unhelpful to diagnose anything, it doesn't mean anything more than "couldn't do it, no idea why". How GetDC() can produce that error code is very hard to guess, I suspect it is environmental with something hooking the winapi function. Perhaps something similar to remote desktop or a screen recorder. Do try running this on another machine.

The "normal" reason for GetDC() failure is a handle leak. Windows stops giving a process more handles when it has consumed 10,000 of them already. Something you can diagnose with TaskMgr.exe, Processes tab. View + Select Columns and tick Handles, USER Objects and GDI Objects. First check the list of processes and verify that you don't have a process that consumes a lot of them. The total number of GDI Objects for all processes in a session is limited by the session pool size. Next run your program and keep an eye on the values for your process.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...