0

Hello my app has a button to print listed documents. The list can be quite long, like 1000 documents.

        PrintDialog printDialog = new PrintDialog();
        DialogResult dialogResult = printDialog.ShowDialog();
        

        if (dialogResult == DialogResult.OK)
        {
            foreach (Datei datei in dateienList)
            {
                if (datei.dateiAuswählen == true) 
                {
                    PrinterSettings currentPrinterSettings = new PrinterSettings();
                    printDialog.PrinterSettings = currentPrinterSettings;
                    currentPrinterSettings.Copies = (short)datei.anzahlKopien;
                    ProcessStartInfo processInfo = new ProcessStartInfo()
                    {
                        Verb = "print",
                        CreateNoWindow = true,
                        FileName = datei.pfad,
                        WindowStyle = ProcessWindowStyle.Hidden
                    };
                    Process process = new Process();
                    process.StartInfo = processInfo;
                    process.Start();
                    process.WaitForInputIdle();
                    process.WaitForExit();
                    System.Threading.Thread.Sleep(5000); 
                }
            }
        }

At first I set Thread.Sleep timeout to 3000, but listed documents got printed out in a mixed order. So, I set it to 60000, assuming it has too little time to process the documents, but it takes way too long since every single document has to be opened, processed, and then closed even though it's set to be invisible.

Is there any solution to this problem? Or should I try another method to print out documents?

LFLJM
  • 65
  • 10
  • 1
    Did you read this: https://stackoverflow.com/questions/5001920/c-sharp-check-printer-status ? – nilsK Feb 09 '23 at 13:25
  • 1
    That is how windows behaves, your application doesn't have a direct link with the printer, it only sends out the command to print something and windows handles it. And since the printer services need information about paper size, layout, formatting, etc. windows must open the file in its native program. – Shiin Zu Feb 09 '23 at 13:27
  • 2
    Why do you have the Thread.Sleep in the first place? You are waiting for Exit anyway. The order of documents printed is set by your printer spool, afaik. So waiting is just a very bad patch to forcing an order in the spooler. I'd investgate if you can set it to "FIFO mode" or something. – Fildor Feb 09 '23 at 13:33
  • 1
    @Fildor I really appreciate your comment. It was indeed the problem, I simply deleted it and it works! Thank you so much again. – LFLJM Feb 13 '23 at 09:47

0 Answers0