简体   繁体   中英

System.Printing Printqueue.AddJob methods consumes memory and doesnt releases once the document is printed

I am Using System.Printing library in my application with .net framework 3.5. When i Print job on any printer using PrintQueue.AddJob memory gets increased and doesnt releases it. Memory is released only if the application is closed. If i print more than 10 jobs then my application uses all the memory of my computer and finally everything gets slows down. I obeserved the memory usage goes to 2GB, which is not acceptable. After investigation i found that Memory gets increased when PrintqQueue.AddJob method is invoked.

Here is my sample Code for printing:

PrintServer printServer = newPrintServer(@"\\sshinde");
PrintQueue PrintQ = newPrintQueue(printServer, "HP_Printer", PrintSystemDesiredAccess.AdministratePrinter);
PrintQ.AddJob("xyz", @"C:\ProgramData\MyComapny\PrintSoftware\Config\Print_Manager\INPUT\d7a8a71b-4e73-43ec-8ee9-bbe24b3ba2cb.xps", false);

What is happening is that the Dispose() method of the PrintQueue is never called.

If this doesn't happen, it won't release any unmanaged resources and will not let the garbage collector clean it up.

For more information see :

http://msdn.microsoft.com/en-us/library/ms584331(v=vs.110).aspx

You can call Dispose() manually when you're done addressing the print queue, or encapsulate it with a using statement like this :

using(PrintServer printServer = newPrintServer(@"\\sshinde"))
{
  using(PrintQueue PrintQ = newPrintQueue(printServer, "HP_Printer", PrintSystemDesiredAccess.AdministratePrinter))
  {
    PrintQ.AddJob("xyz", @"C:\ProgramData\MyComapny\PrintSoftware\Config\Print_Manager\INPUT\d7a8a71b-4e73-43ec-8ee9-bbe24b3ba2cb.xps", false);
  }
}

The Dispose method will then be called once the statements inside the using statement are completed.

The advantage of the using statement, is that it will call the dispose method even if an exception occurs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM