我已经构建了一个可以监控打印作业的C#WPF应用程序。一切正常。我可以打印文档并监视其打印作业。
但每次我点击"打印表格"按钮,触发"打印作业更改"事件,我在“输出”面板中三次收到消息Exception thrown: 'System.NotSupportedException' in PresentationCore.dll
(请参阅下面的更多消息)。起初我认为它来自控制台日志代码,例如Console.Write
或Console.WriteLine
,但是当我检查它们时,我的程序没有。
所以我查看了什么是System.NotSupportedException here。
我明白了
不支持调用的方法时,或者尝试读取,搜索或写入不支持调用的功能的流时抛出的异常。
然后我记得在我的"关于打印工作的变化"事件处理程序,我有一些语句从不同的线程调用对象(我不知道这个术语是什么)。然后我尝试禁用每个有调用的行。但是当应用程序再次运行时,消息仍然存在。
以下是“输出”面板中的完整消息,该消息来自"打印表单"按钮(在第二次点击之后,它没有显示第一行和最后两行):
'PrintClientWPF.vshost.exe' (CLR v4.0.30319: PrintClientWPF.vshost.exe): Loaded 'Anonymously Hosted DynamicMethods Assembly'.
Exception thrown: 'System.NotSupportedException' in PresentationCore.dll
Exception thrown: 'System.NotSupportedException' in PresentationCore.dll
Exception thrown: 'System.NotSupportedException' in PresentationCore.dll
The thread 0x1f18 has exited with code 0 (0x0).
The thread 0x272c has exited with code 0 (0x0).
这是"关于打印作业更改的事件处理程序"事件:
public async void PrintHelperInstance_PrintJobsChange(object sender, EventArrivedEventArgs e)
{
ManagementBaseObject objProps = (ManagementBaseObject)e.NewEvent.Properties["TargetInstance"].Value;
string instanceOperation = e.NewEvent.SystemProperties["__Class"].Value.ToString();
string jobName = objProps["Document"].ToString();
if (JobNameList.Contains(jobName))
{
if (!IsFoundPrintJob)
{
IsFoundPrintJob = true;
}
CurrentJobStatus = (string)objProps["JobStatus"];
if (CurrentJobStatus != PreviousJobStatus)
{
uint jobId = (uint)objProps["JobId"];
string jobDriver = (string)objProps["DriverName"];
string jobHost = (string)objProps["HostPrintQueue"];
string jobStatus = (string)objProps["JobStatus"];
// Use Dispatcher.Invoke to execute statements from a thread other than the main thread
this.Dispatcher.Invoke(() =>
{
string printerName = PrintHelperInstance.GetPrintQueueFromQueueDriverName(jobDriver).Name;
string msg = string.Format("ID: {0}, Name: {1}, Printed on: {2}, Host: {3}, Status: {4}",
jobId, jobName, printerName, jobHost, jobStatus);
TextBlockPrintJobStatus.Text += msg + System.Environment.NewLine;
});
PreviousJobStatus = CurrentJobStatus;
}
}
// Indicates that the print job has been deleted
if (IsFoundPrintJob && instanceOperation == "__InstanceDeletionEvent")
{
this.Dispatcher.Invoke(() =>
{
TextBlockPrintJobStatus.Text += "Print completed" + System.Environment.NewLine;
});
IsFoundPrintJob = false;
string postResponse = "";
try
{
// TODO: Not implemented yet, just testing here
postResponse = await ApiHelperInstance.Post(new KeyValuePair<string, string>("id", "1"));
}
catch (HttpRequestException ex)
{
this.Dispatcher.Invoke(() =>
{
TextBlockPrintJobStatus.Text += "HTTP Request Error: " + ex.Message + System.Environment.NewLine;
});
}
catch (Exception ex)
{
this.Dispatcher.Invoke(() =>
{
TextBlockPrintJobStatus.Text += "Exception: " + ex.Message + System.Environment.NewLine;
});
}
finally
{
this.Dispatcher.Invoke(() =>
{
TextBlockPrintJobStatus.Text += postResponse + System.Environment.NewLine;
});
}
}
}
其实我是C#的新人,而且我对这类事情没有高级知识。
所以我的问题是,为什么会出现异常消息和我该如何摆脱它?