【问题标题】:GetPrintJobInfoCollection() Exception sometimesGetPrintJobInfoCollection() 有时会出现异常
【发布时间】:2013-08-17 02:25:55
【问题描述】:

这段看似无用的代码重现了问题。另一个应用程序正在使用 http://printqueuewatch.codeplex.com/ 将在打印作业发送到打印机时收到通知。 它可以工作,但有时当您发送打印作业时,它会在 GetPrintJobInfoCollection 处崩溃。 我已经粘贴了内部异常。为了重现,我使用 Notepad++ 或我的应用程序发送了大约 20 次小文本文件,直到我崩溃。 如果在崩溃后我调用 GetPrintJobInfoCollection 它会成功运行,或者我重试。

有什么建议可以解决这个问题吗?

while (true)
{
    Thread.Sleep(10);

    LocalPrintServer server = new LocalPrintServer();

    var q = server.GetPrintQueue("vp1");
    q.Refresh();
    // Debug.WriteLine(q.IsBusy);
    PrintJobInfoCollection infos = null;
    infos = q.GetPrintJobInfoCollection();
}

错误

System.NullReferenceException was unhandled   Message=Object reference
not set to an instance of an object.   Source=System.Printing  
StackTrace:
    at MS.Internal.PrintWin32Thunk.AttributeNameToInfoLevelMapping.InfoLevelCoverageList.Release()
    at MS.Internal.PrintWin32Thunk.EnumDataThunkObject.GetPrintSystemValuesPerPrintJobs(PrintQueue
printQueue, Queue`1 printObjectsCollection, String[] propertyFilter,
UInt32 firstJobIndex, UInt32 numberOfJobs)
    at System.Printing.PrintJobInfoCollection..ctor(PrintQueue printQueue, String[] propertyFilter)
    at System.Printing.PrintQueue.GetPrintJobInfoCollection()
    at WpfApplication7.MainWindow.button2_Click(Object sender, RoutedEventArgs e) in

【问题讨论】:

    标签: c# .net printing xps xpsdocument


    【解决方案1】:

    根据this MSDN article,您不应使用 System.Printing 命名空间。

    不支持使用 System.Printing 命名空间中的类 在 Windows 服务或 ASP.NET 应用程序或服务中。尝试 从这些应用程序类型之一中使用这些类可能 产生意想不到的问题,例如服务性能下降 和运行时异常。如果您想从 Windows 窗体打印 应用程序,请参阅 System.Drawing.Printing 命名空间。

    我认为您的问题是由于资源泄漏造成的。 LocalPrintServer 类似乎是非托管资源,需要被释放。

    【讨论】:

      猜你喜欢
      • 2015-02-13
      • 1970-01-01
      • 1970-01-01
      • 2013-01-13
      • 2018-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多