GetPrintJobInfoCollection() Exception sometimes
Asked Answered
O

1

6

This code that might seem useless it reproduces the problem. Another application is using http://printqueuewatch.codeplex.com/ to be notified when a print job is sent to printer. It works but sometimes when you send a print job it crashes here GetPrintJobInfoCollection. I have pasted the inner exception. To reproduce I send with Notepad++ or my application a small text file about 20 times until i get a crash. If after the crash I call GetPrintJobInfoCollection it works successfully or I retry.

Any suggestion how to fix this ?

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();
}

Error in

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
Osmen answered 14/8, 2013 at 22:40 Comment(0)
A
1

According to this MSDN article you shouldn't use System.Printing namespace.

Classes within the System.Printing namespace are not supported for use within a Windows service or ASP.NET application or service. Attempting to use these classes from within one of these application types may produce unexpected problems, such as diminished service performance and run-time exceptions. If you want to print from a Windows Forms application, see the System.Drawing.Printing namespace.

I am thinking that your problem is due to resource leak. The LocalPrintServer class seems to be an unmanaged resource and needs to be disposed.

Airdrop answered 14/9, 2016 at 21:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.