How to get an entire document list printed 'in order' in C#

94 Views Asked by At

here is my code to print documents in a binding list

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

        if (dialogResult == DialogResult.OK)
        {

            foreach (Datei datei in dateienList)
            {
                if (datei.dateiAuswählen == true) //if selected
                {
                    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); 
                }
            }
        }

The problem is that documents are not printed in order if the number of documents increases like 100 or more. After some are printed in the right order, documents are printed in a mixed order, which I want to prevent. I haven't figured out why but my assumption is the size of documents causes the problem.

So I thought maybe I can use a property in ProcessStartInfo class to set a range to the whole list of documents avoiding foreach loop, which would make the order more stable but not really successful so far. Is there any way to fix the problem?

1

There are 1 best solutions below

0
LFLJM On

The problem was Thread.Sleep. I deleted this line and it works without any delay and prints out in order.