WPF default behavior on exceptions - c#

Please note that I'm aware of possible solution and have read some other SO posts which will answer the question how to handle unhandled exceptions globally and I'm not asking if it is good practice or not.
What I'm asking is:
Why do WPF applications close on exceptions?
I always thought that it is the expected behaviour, however when researching how to implement the exception handling globally in WPF, I encountered this sentence on the Microsoft documentation page:
By default, Windows Presentation Foundation catches unhandled exceptions, notifies users of the exception from a dialog box (from which they can report the exception), and automatically shuts down an application.
But my experience is different, the application just exits without any warning, so any unhandled exception will be a total mystery for the user and me as a developer.
My question is really: Am I missing something, or is Microsoft wrong about their own framework?

The docs seems to be wrong.
By default, a WPF application exits without any dialog box when an unhandled exception is thrown on the dispatcher thread.
You may consider to edit the docs and submit a PR on GitHub by clicking on the "Edit" button in the top right corner of the page.

Related

Object already in use elsewhere

When I am drag picture Box in Windows form it's showing as Object already in use elsewhere.
I searched over Stack overflow and other website says that error is Threads, Coding oriented and GDI+. But I didn't write any coding in that. I just drag the picture box. Then it shows “already in use elsewhere”.
Anyone Explain?
This is usually an indicator that something else, potentially some other thread in your own application, already has the target file that you're trying to save locked at the file system level. If you look at the inner exception I believe it should mention this. If it's not directly in the InnerException Another way to confirm this (or discover what it might really be instead) is to turn on first chance exceptions in the debugger and watch for what exception is being thrown "underneath" Save and then being turned into this generic exception.
A Generic Error occured at GDI+ in asp.net mostly because of missing target folder / access permissions.

Using A Custom Exception Form

I'm currently working on a C# WinForm application, and am trying to create a custom Form to use whenever an uncaught exception is thrown. The reason for this custom form, is to be able to log the details of every thrown exception in a log file, as well as provide the user with a sharp looking GUI with better, and easy to understand details of the error that occurred.
As it stands right now, i am registering for exception events:
Application.ThreadException += new ThreadExceptionEventHandler(ExceptionHandler.OnThreadException);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(ExceptionHandler.OnUnhandledException);
The ExceptionHandler class logs the error in a log file, and then displays my custom WinForm. My question is, is this the only way to display my custom form? An issue i know of right now, is i am unable to determine whether the application can still continue, or if it will close when the form is closed.
Overall, my question is... Is there a better, or easier way to use my custom exception form? Also, is there a way to know IF the application will be able to recover, once the Exception form is closed?
It's not safe to continue running a program if you don't understand the reason for an exception.
This topic is discussed in more detail here:
http://blogs.msdn.com/b/codeanalysis/archive/2006/06/14/631923.aspx
You should start by exiting the application for any error you do not anticipate. The form is a nice touch but beware it may not work, since you do not know what the problem is - so wrap that in a try catch. Then as errors come up do some analysis and if it is something you can recover from than catch that specific exception and recover. Typically though if something bubbles all the way up to AppDomain.CurrentDomain.UnhandledException it is probably something that will leave the app in a bad place and you want to close.

Regarding ThreadExceptionDialog dialog

I know that the ThreadExceptionDialog is thrown for all exceptions that occur in the program.
My problem is that the dialog form shows assembly information along with the Exception details.
Is there a way to hide the assembly information and show only the exception information?
You can "disable" this behavior completely by calling Application.SetUnhandledExceptionMode() in the Main() method of your application. As far as I know, there isn't a way to hide just the assembly information from the default ThreadExceptionDialog. However, if you set your application to handle ThreadException, you can display your own information. If you don't override it, you get the normal ThreadExceptionDialog and the user may quit or continue. Continuing is not a good outcome 99.99% of the time.
Please review the MSDN document for more information.

Non-Blocking Messagebox & DllImport Drawbacks

I am currently a developer working on a very task specific application; the user(s) are often multitasking so they can often make errors inputting data. The nature of the task makes notifying them of those imputation mistakes important. We currently use a combination of panels and labels and/or a small label at the bottom of the application in order to notify them of something happening. In the near future we will be tasked to update interface for the application.
Recently while looking up an unrelated question I came across the following question:
click here for link the answer caught my eye and setting the owner’s handle to zero (null) did indeed do what the author asked.
Since I have only recently started working with importing and working with unmanaged code in C# I was wondering if there were any “side effects” of using this method. I of course realize that the user could cause the application to open a ton of message box(s). I have in the past attempted to use a thread-safe message box and found the requirement of our application always being on top was an issue (the location of the message box was hidden by the application).
I was able to take to test the code against the requirement, and it appears to have similar issues as the method I found but appears we could work around those issues. I did think of something that could be done, I realy need to figure out how to determine where on the screen the message box will be displayed.
In order to address Jim's concerns one has to understand that the application that is running is processing data continously. So currently anything that locks the main thread, can cause problems processing this data, which is task critical. We get around this problem using labels, but with our future task I am looking for a more streamline approach to present these error messages. The main problem is the solution also must not additional overhead to the application. Some additional tid-bits of information is that the system is used on a closed network.
I suppose the short question would have to be, are there any negatives of using this particular method, to show a non-blocking messagebox in C#?
There's no particular technical problem with displaying modal message boxes by setting the window handle to null. I question its use as a UI error message indication, because it doesn't prevent the user from continuing in the face of error.
Modal message boxes that make the user hit OK and then correct the error make for a terribly clunky user interface. But a popup that might be hidden and lets the user keep going is just as bad. There are much better solutions, some of which are shown in the validation examples for Windows Forms (and, presumably WPF).

winform application exception DragDrop occurs outside of IDE

My winform application is failing when run outside of the IDE with a dragdrop exception. The error does not occur while being run from inside the ide (VS2008). How can I trace this. I have seen mention of using JIT in the error box that is displayed post error - will this help me trace my problem?
Don't know if this will be helpful, but...awhile ago I was getting DragDropException when my WinForms app was being run on a thumb drive, not a full system. The form generating the error had no drag-and-drop feature. I never figured out the problem, but I saw that a UserControl on that form had defaulted AllowDrop=true unnecessarily. Once I turned that off, the problem went away.
If your form is intended to support drag-and-drop, I'd look into on what thread the form generating the exception is instantiated. My understanding is that Microsoft's implementation of drag-and-drop is COM-based, which must be initialized in an STA thread. Putting the [STAThread] attribute before your program's entry point will accomplish this.

Categories