Application.Dispatcher.UnhandledException and CurrentDomain.UnhandledException - wpf

I'm investigating strange WPF multi threaded application hang that happen randomly on production machines more easily than development ones.
I have added Application.Dispatcher.UnhandledException and CurrentDomain.UnhandledException event handlers to log the every and each exception in order to keep the application from crashing as it is a system shell.
I suspect the reason of the hang is that I'm handling those events and not allowing the application to crash, what make me say this is that when i monitored the application activity when it hanged using windows task manager i noticed CPU load probably caused by background tasks in the application and those background tasks are writing to logs as expected.
The logger class writes the logs to the disk asynchronously using BeginInvoke.
The big problem is every time the application hangs in production machine i find nothing in log files.
Now i have two questions:
What is the fire order of those events: "Application.Dispatcher.UnhandledException and CurrentDomain.UnhandledException".
2.Is there anyway i can manage to be able to know were my code is failing
Note: I use a lot of Thread.Sleep in background tasks as i manage the threads my self and i think i went wrong with this approach and I'm considering rewriting all of my background tasks from scratch.
Any help will be much appreciated.

Order of this event is:
Application.DispatcherUnhandledException
CurrentDomain.UnhandledException
If you want get information from exception where your code is failing, check property StackTrace. You can also attach to your project pdb files and in this case you will get StackTrace with line numbers.

Related

WPF application crashes when run in Jenkins service but not as a regular user

I am automating some software testing using Jenkins on our in-house test software. It is written in C# with WPF. I am using the version of the program that has no frontend, but it still makes calls to WPF in order to start the service. The test application is launched through python scripts using subprocess.Popen
When running under the Jenkins slave process I get the following error:
Unhandled Exception: System.ComponentModel.Win32Exception: The operation completed successfully
at MS.Win32.UnsafeNativeMethods.RegisterClassEx(WNDCLASSEX_D wc_d)
...
From my research it seems that something is going wrong with the windows atom tables. The weird thing is that I don't run into the same issue when running the python scripts as a local user, it only crashes when it is the Jenkins service running the scripts.
Is there some limitation on the local atom table for windows services?
Is Jenkins hogging atom table entries?
Update 1:
I did some more research on the crash and some resources said for WPF there might be a windows handle leak, so I inserted some power shell calls to check how many active handles there are on the system.
It doesn't look like there is a handle leak from any of the software under test, I am seeing around 50-60k handles while jenkins is running the test scripts, which is consistent with what I was seeing while running it under my user account. It looks like Jenkins is interfering with WPF's ability to construct the program (The full callstack for the error is on the Main() constructor), but I have no idea why it would be doing that.
Update 2:
Some extra information since I think it is relevant, here is the full call stack:
Unhandled Exception: System.ComponentModel.Win32Exception: The operation completed successfully
at MS.Win32.UnsafeNativeMethods.RegisterClassEx(WNDCLASSEX_D wc_d)
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()
at System.Windows.Application..ctor()
at <TestingApplication>.ScriptHost.App..ctor()
at <TestingApplication>.ScriptHost.App.Main()
So what appears is happening is the following:
Jenkins calls the python scripts. They perform setup and all pre-condition work such as bringing up the software under test
Python calls the TestingApplication through Popen
The executable starts and attempts to construct the application
WPF checks to see if there is a Dispatcher already on the thread, there is not
WPF attempts to create a Dispatcher since one doesn't already exist
Crash because a Dispatcher cannot be created
Again, this only happens when launched under the Jenkins service user.
So here's what I did to fix the problem:
The software under test also uses WPF to create the displays, so I wanted to see if maybe i'm running out of resources by having too many things open. There are 5 SUT applications. Luckily, one of the pieces of software we run is a console that I don't need open when running tests through Jenkins. Thus I was able to not launch the console application and that freed up enough resources for the test application to run.
What this doesn't answer is:
Why do I have less resources on a service compared to a regular user?
Is this because of Jenkins/Python or because of Windows?
Is there a workaround for this problem?
Those questions could be asked outside of this question so I am answering just so that it is available to someone with a similar problem and to close it. For someone who doesn't have the luxury of closing extra applications, I would ask those questions yourself.

Embedding CEF3 with existing application

I have a running WIN32 application. There a window in this application where I want to show web content using CEF3. But, I am facing problems and the entire window becomes white without showing any web page content. So I have the following questions:
Is it possible to use CEF3 with existing message loop in application? I dont want to call the CEF message loop, it may impact other things in my application.
Is it absolutely necessary to use a message window as in the sample application? I am not able to understand its objective.
When CEF3 launches multiple processes, how does it show in the task manager? If my application name is A.exe, does it show A.exe multiple times in task manager?
Any help is much appreciated.
For windows users there is possible to use multi threaded message loop (CefSettings). It is allow maintain browser windows via own message loop. But there is good practice use single threaded message loop, - you can call CefDoMessageLoopWork periodiacally on idle or some additional events. It is possible even with existing message loop.
I'm not sure what you mean.
CefSettings.BrowserSubprocessPath specifies which executable will be used for child processes. While you are integrating it in other process, looks like it is one possible solution and in task manager you will see processes as you named it.
About the question number 2:
every windows application has its own "main window" and wndProc that receives all the messages sent by his children.
And the sample win32 cefclient shows how to integrate cef message loop inside the application's message loop.
And if you don't handle and dispatch cef messages in proper way the browser window becomes white.

How to determine which form, event, or method in WinForm app that's running to debug

This scenario has come up before and I'm wondering if there is any way I can determine where the actively running form is executing within code? The problem is when I inherit a very large application which I'm not totally familiar with yet and I have it running through VS.NET 2010. I might have a particular screen up and go "geeze it would be nice if I could start debugging when I do 'x'".
If this was a simple form with some buttons I wouldn't even bother asking here; I'm not that novice. But the time consuming task is when I look at a tabbed screen in a large multi-project solution with drag and drop capabilities, right click options, etc. and have to spend 5-10 minutes tracking down where to place a breakpoint to debug.
What I'm wondering is if there is a way to have the WinForms app running via IDE and do 'something' that tells VS.NET on the next action break into the code (obviously without a breakpoint because I don't know where to place one yet). This would save me a ton of time trying to track down which event is occurring in a not so simple form or series of forms.
I hope this makes sense...
Thanks!
Yes, that's somewhat possible. When you use Debug + Break All then the 99.9% odds are that you don't break into code that's part of the project. A Winforms app is normally idle, pumping the message loop and waiting for Windows to tell it that something happened. You'll break at the Application.Run() statement.
The trick to then use Debug + Step Over. The program resumes running like normal. Then give a UI command (do 'x' in your question) and the debugger will break at the first statement of real code, typically at the start of the event handler for that command. It isn't exactly guaranteed that that code would be relevant, you might break at a MouseMove event handler for example. So YMMV.

Problem in process hooking

I have a process (say, for example, MyProcessA), hooked an exe and injected my dll (MyDll.dll) into the process space of MyProcessA, so even if it's gonna create n number of child processes it will be process hooked as well. I have no problem in hooking and injecting the dll into the process. I have hooked all file and process dependant functions, but somehow I am not able to achieve complete hook of any setup (any application setup). I suspect if am missing any process related APIs or it might be some UAC problem, currently I am using CreateProcess(A&W), NtCreateProcess, ShellExecute(A&W). What could be the problem?
I suspect that the answer is related to the "Windows Installer Service". I'm guessing that your hooks wouldn't catch any interactions with a service, which even if launched as a result of FireFox's setup is going to be created by a different System process. I haven't had much experience with Windows Installer, but the documentation here should have more details than you could possibly wish for, given the time to find it.
UAC might also cause you issues, but you should be able to rule that out by launching the hooking code with administrative privileges to start with.
Is this research for uni? Either way good luck, it sounds like an interesting problem.

WPF application freeze

I am kind of lost here, and I don't know what to do
I have a problem that I don't know what the source of it.
We have a large wpf application, that is built similar to prism (composite application)
Actually we are using lots of prism library.
I wrote a module to replace an existing module.
And the application now is loading my module, instead of the old module.
I start to notice, with the new module, that sometimes the application freeze for 20-30 seconds without any response, and then it work smoothly after that.
The freeze is not consistent, and there is no pattern or a specific reason that cause it.
I am suspecting that could be my module that is causing that freeze.
But at the same time, many other developers introduced new code to the application.
My question is there a way to trace that freeze?
Is there a way to compare the old module and the new module that I wrote?
I can run the application with my module, and trace the application, and then I can change the config file to load the old module.
Is there a way to compare between the two?
Should I do profiling?
and if profiling will help, then what should I look for?
any other tool could help?
Thanks a lot for any reply
It sounds like you are doing a long running operation on your UI thread. Are you making any database / webservice calls from the main thread? I think one of the easiest things to do is run it in the debugger, and when it freezes, hit the "pause" button. Visual Studio will pause execution at the current location, and you can examine what is taking so long.
If the problem isn't immediately obvious there, I would definitely start profiling the application to track this down.

Resources