多线程 UI 导致 WPF 应用程序停止工作
Multi-thread UI causing WPF application to stop working
我的应用程序中的弹出窗口 window 需要使所有其他 windows 模式,除了显示 PDF 文档的那个。经过一些搜索,我发现如果 PDF window 在另一个线程上,弹出窗口 window 将不会禁用它。但是,当另一个线程的 PDF window 因任何原因引发异常时,用户会收到 "application has stopped working" 并且整个应用程序会被 Windows 关闭。即使线程在 try-catch 块中。难道我做错了什么?为什么异常导致 Windows 关闭应用程序?
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();
}
}
正如评论者 stijn 指出的那样,您将 try/catch
放在了错误的位置。改为这样做:
public static void OpenPdfDocument(string pdfPath)
{
Thread pdfDocuThread = new Thread(
new ParameterizedThreadStart(OpenPdfHelper));
pdfDocuThread.SetApartmentState(ApartmentState.STA);
pdfDocuThread.IsBackground = true;
pdfDocuThread.Start(pdfPath);
}
private static void OpenPdfHelper(object pdfPath)
{
try
{
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();
}
}
catch (Exception ex)
{
Mouse.OverrideCursor = null;
AppErrorLog.LogError("PDFTHREADERROR: " + ex.Message);
}
}
我的应用程序中的弹出窗口 window 需要使所有其他 windows 模式,除了显示 PDF 文档的那个。经过一些搜索,我发现如果 PDF window 在另一个线程上,弹出窗口 window 将不会禁用它。但是,当另一个线程的 PDF window 因任何原因引发异常时,用户会收到 "application has stopped working" 并且整个应用程序会被 Windows 关闭。即使线程在 try-catch 块中。难道我做错了什么?为什么异常导致 Windows 关闭应用程序?
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();
}
}
正如评论者 stijn 指出的那样,您将 try/catch
放在了错误的位置。改为这样做:
public static void OpenPdfDocument(string pdfPath)
{
Thread pdfDocuThread = new Thread(
new ParameterizedThreadStart(OpenPdfHelper));
pdfDocuThread.SetApartmentState(ApartmentState.STA);
pdfDocuThread.IsBackground = true;
pdfDocuThread.Start(pdfPath);
}
private static void OpenPdfHelper(object pdfPath)
{
try
{
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();
}
}
catch (Exception ex)
{
Mouse.OverrideCursor = null;
AppErrorLog.LogError("PDFTHREADERROR: " + ex.Message);
}
}