locked
Printersettings are very slow RRS feed

  • Question

  • Hi at all,

     

    my question is about the printersettings in c#.

    When i call the PaperSizes or the PaperSource the system are very slow.

    My issue is to search in the Papersizes or in the papersource for the correct size or source.

     

    Code:

            private PaperSize GetPaperSize(PrinterSettings printerSettings)
            {
                PaperSize paperSize = new PaperSize();

                // Set the PaperSize
                for (int i = 0; i < printerSettings.PaperSizes.Count; i++)
                {
                    if (printerSettings.PaperSizes[i].PaperName.Contains(reportPrinterSettings.PaperSize))
                    {
                        paperSize = printerSettings.PaperSizes[i];
                        break;
                    }

                    paperSize = printerSettings.PaperSizes[0];
                }

                return paperSize;
            }

     

    Can anyone help me?

    Thanks beni

    • Moved by edhickey Monday, August 1, 2011 2:21 PM (From:.NET 3.0/3.5 Windows Workflow Foundation)
    Monday, August 1, 2011 9:45 AM

Answers

  • I had the same problem. Changing the for loop to a foreach loop resolved the issue.

     

    Example:

        foreach (PaperSize paperSize in printerSettings.PaperSizes)
        {
            // your code here
        }

    • Marked as answer by Lie You Thursday, November 3, 2011 2:20 AM
    Friday, October 28, 2011 6:25 PM

All replies

  • What is the number PaperSizes.Count and how long does the loop take?

    chanmm


    chanmm
    • Marked as answer by BeniLeni Thursday, August 4, 2011 6:33 AM
    • Unmarked as answer by BeniLeni Thursday, August 4, 2011 6:33 AM
    Tuesday, August 2, 2011 6:57 AM
  • Hi chanmm thx for your answer,

    the count of the papersizes are 41 and the loop take 15,4 seconds.
    This is definitely to slow for the application.
    Especially i take the same loop for the PaperSources -_-
    Do you know a solution?

    thx beni
    Thursday, August 4, 2011 6:33 AM
  • Pass in and iterate through PaperSizes instead of PrinterSettings.
    Thursday, August 4, 2011 8:18 AM
  • Hi John,

    you have right the speed is now 9 seconds, but this is still to slow.

    Now my code looks like this:

            private PaperSize GetPaperSize(PrinterSettings printerSettings)
            {
                PaperSize paperSize = null;

                PrinterSettings.PaperSizeCollection collection = printerSettings.PaperSizes;

                // Set the PaperSize
                for (int i = 0; i < collection.Count; i++)
                {
                    if (reportPrinterSettings.PaperSize == null)
                    {
                        paperSize = collection[0];
                        break;
                    }

                    if (collection[i].PaperName.Contains(reportPrinterSettings.PaperSize))
                    {
                        paperSize = collection[i];
                        break;
                    }

                    paperSize = collection[0];
                }

                return paperSize;
            }
    Thursday, August 4, 2011 9:05 AM
  • This is the best that can be done with the posted  code:

        private PaperSize GetPaperSize(PrinterSettings printerSettings)
        {
          
    PrinterSettings.PaperSizeCollection collection = printerSettings.PaperSizes;
          
    PaperSize paperSize = collection[collection.Count - 1];
          
    if (reportPrinterSettings.PaperSize != null)
          {
            
    string paperName = reportPrinterSettings.PaperSize;
            
    for (int i = 0; i < collection.Count; i++)
            {
              
    if (collection[i].PaperName.Contains(paperName))
              {
                paperSize = collection[i];
                
    break;
              }
            }
          }
          
    return paperSize;
        }


    How are you timing this code?  Show the code that calls this method.



    Thursday, August 4, 2011 10:04 AM
  • Try this code:

        private PaperSize GetPaperSize(PrinterSettings printerSettings)
        {
          
    Stopwatch sw = Stopwatch.StartNew();
          
    PrinterSettings.PaperSizeCollection collection = printerSettings.PaperSizes;
          
    PaperSize paperSize = collection[0];
          
    if (paperName != null)
          {
            paperName = collection[collection.Count - 1].PaperName;
            
    for (int i = 0; i < collection.Count; i++)
            {
              
    if (collection[i].PaperName.Contains(paperName))
              {
                paperSize = collection[i];
                
    break;
              }
            }
          }
          sw.Stop();
          
    Console.WriteLine(sw.ElapsedMilliseconds);
          
    return paperSize;
        }

    I  get  15 ms for a local printer and 150 ms for a local network printer.
    Thursday, August 4, 2011 10:29 AM
  • thx John, the ellpased time are now by 4 seconds, this is still slow but i think

    this is the driver of the printer which are so slow.

    Or what do you mean?

    Thursday, August 4, 2011 11:19 AM
  • What do you get with the benchmark code on the XPS printer and other printers?

    Thursday, August 4, 2011 12:22 PM
  • Hi John,

    thanks for your answer. I have now tested the application and i found the reason.

    The reason is the name of the printer. If the printername is \\WORKGROUP01\TOSHIBA e-STUDIO4520CSeries the ellapsed time is 1,2 seconds

    but if the printername is \\WORKGROUP01.company.local\TOSHIBA e-STUDIO4520CSeries the ellapsed time is 4,9 seconds.

    Now i have the reason but not the solution ;)

    The requirement is that the printername is allways the second choice, also the printername over the network.

     

    Do you know how i can make it faster?

     

    thx beni

    Friday, August 5, 2011 5:52 AM
  • Why the printername is allways the second choice? Is it the domain name?

    I guess this "\\WORKGROUP01.company.local\TOSHIBA e-STUDIO4520CSeries" is in the domain. and this "\\WORKGROUP01\TOSHIBA e-STUDIO4520CSeries " is in your develop machine. when the applicatio run in your machine,it's good. But in the domain, because the app will search and connect the dirve, it will be little slower.Also the net work issue is also the one of the reasons.

     

     


    Best Regards,
    Rocky Yue[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Monday, August 8, 2011 2:44 AM
  • Hi Rocky Yue,

     

    we must allways use the second choice of the full domainname.

    Because the interface of the customers application send us the full name as printername.

     

    One solution is to remove the domain postfix after we get the printername,

    but this is not sure wether the printername will be always like that. I think that is not a good solution for my problem.

     

    Do you know a better way to solve this problem?

     

    thx Beni

    Wednesday, August 31, 2011 5:32 AM
  •  Install the drive to the local machine.

    If you were not install, just search printername from domain. it must waste lots of time on searching.

     


    Lucky Dog
    Wednesday, August 31, 2011 6:01 AM
  • hi qqaiqq,

    the printerdriver are installed on the local machines.

    Call the settings only with the printername does not working.

     

    thx beni

    Wednesday, August 31, 2011 8:50 AM
  • Have you got why it slow?
    Lucky Dog
    Wednesday, August 31, 2011 8:59 AM
  • It seems like a network problem of the printersettings.

    I made a speed measurement and the result looks like this:

    Print without full domainname (Printername "\\servername\toshiba-e-studio4520cseries) "):
        Get the PaperSize         -> 1,393
        Get the PaperSource      -> 0,943

    print with full domainname (Printername ". \\servername.company.local\toshiba-e-studio4520cseries"):  
        Get the PaperSize         -> 4,905
        Get the PaperSource     -> 3,637

    Wednesday, August 31, 2011 9:14 AM
  • the measurement is in seconds
    Wednesday, August 31, 2011 9:14 AM
  • It must be the full domain name.

    it will lose some time when app search servername.company.local.

    So i have no good solution for you. My company is also slow when I am printing.

    Try the http://social.msdn.microsoft.com/Forums/en-US/netfxnetcom/threads.

    Not sure if it is a good place to provide a solution. But 3-4 seconds, we can accept it.

     

    Good luck.


    Lucky Dog
    Wednesday, August 31, 2011 9:21 AM
  • Thank you for your response qqaiqq.

    I will try it in the other forum.

    Wednesday, August 31, 2011 9:27 AM
  • I had the same problem. Changing the for loop to a foreach loop resolved the issue.

     

    Example:

        foreach (PaperSize paperSize in printerSettings.PaperSizes)
        {
            // your code here
        }

    • Marked as answer by Lie You Thursday, November 3, 2011 2:20 AM
    Friday, October 28, 2011 6:25 PM
  • Thanks for sharing your solution here.


    Best Regards,
    Rocky Yue[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, November 3, 2011 2:20 AM