I have a WPF MVVM application. It uses .NET Framework 3.5 and it is programmed in C#.
I'm trying to debug my application. I started it directly from VS IDE in debug mode by pressing F5. Once launched from Visual Studio 2008 IDE and when my application crashes, VS doesn't break the application to see the code that is causing it, it only shows dialog box with message that vshost32.exe has stopped working. No exception name and call stack appears so it is difficult to detect the line of code that is causing this behavior.
From Debug->Exceptions menu in VS IDE I don't know which exception I have to check in order to get the program stops at the line causing the error when crashing.
Also I have done below in my app.xaml.cs to capture unhandled exceptions as explained here and here, but none of this exceptions are thrown in my case:
Log log = Log.GetInstance();
public App()
{
Startup += new StartupEventHandler(App_Startup); // Can be called from XAML
DispatcherUnhandledException += App_DispatcherUnhandledException; //Example 2
TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException; //Example 4
System.Windows.Forms.Application.ThreadException += WinFormApplication_ThreadException; //Example 5
}
void App_Startup(object sender, StartupEventArgs e)
{
//Here if called from XAML, otherwise, this code can be in App()
AppDomain.CurrentDomain.FirstChanceException += CurrentDomain_FirstChanceException; // Example 1
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; // Example 3
}
// Example 1
void CurrentDomain_FirstChanceException(object sender, System.Runtime.ExceptionServices.FirstChanceExceptionEventArgs e)
{
MessageBox.Show("1. CurrentDomain_FirstChanceException");
//ProcessError(e.Exception); - This could be used here to log ALL errors, even those caught by a Try/Catch block
}
// Example 2
void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
MessageBox.Show("2. App_DispatcherUnhandledException");
log.ProcessError(e.Exception);
e.Handled = true;
}
// Example 3
void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
MessageBox.Show("3. CurrentDomain_UnhandledException");
var exception = e.ExceptionObject as Exception;
log.ProcessError(exception);
if (e.IsTerminating)
{
//Now is a good time to write that critical error file!
MessageBox.Show("Goodbye world!");
}
}
// Example 4
void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
{
MessageBox.Show("4. TaskScheduler_UnobservedTaskException");
log.ProcessError(e.Exception);
e.SetObserved();
}
// Example 5
void WinFormApplication_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
MessageBox.Show("5. WinFormApplication_ThreadException");
log.ProcessError(e.Exception);
}
From above exception handlers, TaskScheduler.UnobservedTaskException is not supported in NET Framework 3.5 as this uses namespace System.Threading.Tasks not present in NET Framework 3.5, it is only supported from NET Framework 4.0 and above so I have only implemented the others.
Also I have found other interesting articles like below:
- Unhandled Exception Handler For WPF Applications
- Handling exceptions in WPF
- Shutting Application Down after Handling Unhandled Exception
Despite implementing all above 5 exception handlers (except TaskScheduler_UnobservedTaskException in my case), when my WPF application crashes and displays the message that vshost32.exe has stopped working, none of the 5 exception handlers implemented are thrown so I guess some other kind of exception is thrown, but which one? I thought that with these 5 exception handlers all the WPF and winform exception (if using winforms in wpf) would be catched but I see that it is not the case.
ADDITIONAL INFO: This WPF application uses a third party DLL to print PDFs. This is PDFiumViewer:
This Library is fully compatible with Windows 8 as stated in its page (I am using Windows 8 x64).
I use it to print PDFs as explained here:
public bool PrintPDF(
string printer,
string paperName,
string filename,
int copies)
{
try {
// Create the printer settings for our printer
var printerSettings = new PrinterSettings {
PrinterName = printer,
Copies = (short)copies,
};
// Create our page settings for the paper size selected
var pageSettings = new PageSettings(printerSettings) {
Margins = new Margins(0, 0, 0, 0),
};
foreach (PaperSize paperSize in printerSettings.PaperSizes) {
if (paperSize.PaperName == paperName) {
pageSettings.PaperSize = paperSize;
break;
}
}
// Now print the PDF document
using (var document = PdfDocument.Load(filename)) {
using (var printDocument = document.CreatePrintDocument()) {
printDocument.PrinterSettings = printerSettings;
printDocument.DefaultPageSettings = pageSettings;
printDocument.PrintController = new StandardPrintController();
printDocument.Print();
}
}
return true;
} catch {
return false;
}
}
This library uses and requires native PDFium libraries from here. I am using below version: PdfiumViewer.Native.x86.v8-xfa since I am using Windows 8 and I do not want support for Windows XP. Furthermore in my Visual Studio 2008 project, I have two folders x86 and x64 and within each one the correct version, PdfiumViewer.Native.x86.v8-xfa and PdfiumViewer.Native.x86_64.v8-xfa respectively since it is necessary these libraries to be put in these folders (or in the root project folder) to be found and work correctly.
The PDFiumViewer and native PDFium libraries, I have obtained using nuget package and Visual Studio 2015 since Visual Studio 2008 does not support nuget. Once I have obtained these DLLs I have referenced (PDFiumViewer) and copied (PDFium libraries) to my Visual Studio 2008 project and put them in the correct folders.
Finally, I must say that this problem is not only caused from within Visual Studio 2008 IDE when debugging, it is also happening when running the application normally (i.e. outside the Visual Studio debugger). Furthermore, the exception " vshost32.exe has stopped working" sometimes appears soon or appears later.
Also this problem was not happening before adding this third party and native libraries (PDFiumViewer and PDFium libraries) so maybe this is the problem since PDFiumViewer is calling unmanaged DLLs using dllimport statement to import native methods from PDFium, as an example:
private static class Imports
{
[DllImport("pdfium.dll")]
public static extern void FPDF_AddRef();
[DllImport("pdfium.dll")]
public static extern void FPDF_Release();
[DllImport("pdfium.dll", CharSet = CharSet.Ansi)]
public static extern IntPtr FPDF_LoadCustomDocument([MarshalAs(UnmanagedType.LPStruct)]FPDF_FILEACCESS access, string password);
[DllImport("pdfium.dll", CharSet = CharSet.Ansi)]
public static extern IntPtr FPDF_LoadMemDocument(SafeHandle data_buf, int size, string password);
// And some other ones, not posted here to simplify.
}