1
在我的应用程序中弹出窗口需要使所有其他窗口模式,除了显示PDF文档的窗口。经过一番搜索之后,我发现如果PDF窗口在另一个线程上,一个弹出窗口不会禁用它。但是,如果在其他线程的PDF窗口上因任何原因引发异常,则用户将收到“应用程序已停止工作”,并且整个应用程序将被Windows关闭。即使线程处于try-catch块中。难道我做错了什么?为什么导致Windows关闭应用程序的异常?多线程UI导致WPF应用程序停止工作
public static void OpenPdfDocument(string pdfPath)
{
try
{
Thread pdfDocuThread = new Thread(new ParameterizedThreadStart(OpenPdfHelper));
pdfDocuThread.SetApartmentState(ApartmentState.STA);
pdfDocuThread.IsBackground = true;
pdfDocuThread.Start(pdfPath);
}
catch (Exception ex)
{
Mouse.OverrideCursor = null;
AppErrorLog.LogError("PDFTHREADERROR: " + ex.Message);
}
}
private static void OpenPdfHelper(object pdfPath)
{
if (pdfPath is string)
{
DisplayPdfWindow pdfViewer = new DisplayPdfWindow();
pdfViewer.Loaded += (s, ev) => { pdfViewer.SetPdf(pdfPath.ToString()); };
pdfViewer.Closed += (s, ev) => { pdfViewer.Dispatcher.InvokeShutdown(); };
pdfViewer.Show();
Dispatcher.Run();
}
}
可能是因为*线程创建*在try catch块中,而不是*线程函数*即OpenPdfHelper:它的例外情况没有捕获 – stijn