GetPrintJobInfoCollection () exception sometimes

This code, which may seem useless, reproduces the problem. Another application uses http://printqueuewatch.codeplex.com/ to receive notification when a print job is sent to the printer. It works, but sometimes when you submit a print job, it gets reset here GetPrintJobInfoCollection. I threw in an internal exception. To reproduce, I send with Notepad ++ or my application a small text file about 20 times until I get a crash. If after an accident I call GetPrintJobInfoCollection, it works successfully or I repeat.

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

A mistake 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 
+6
source share
1 answer

According to this MSDN article, you should not use the System.Printing namespace.

Classes in the System.Printing namespace are not supported for use in a Windows service or application or ASP.NET service. Attempting to use these classes from one of these types of applications can cause unforeseen problems, such as reduced service efficiency and runtime exceptions. If you want to print applications from Windows Forms, see the System.Drawing.Printing namespace.

I think your problem is with a resource leak. The LocalPrintServer class appears to be an unmanaged resource and must be removed.

+1
source

Source: https://habr.com/ru/post/951761/


All Articles