0

I am trying to print out the message "printing..." to the txtMessage.Text text box before the loop runs but it never does print to the text box before the loop runs. Any idea why?

else
            {
                txtMessage.Text = "Printing...";
                for (int i = numberCopies; i != 0; i--)
                {
                    int paper = Convert.ToInt32(lblPaperAmount.Text);
                    paper--;
                    if (paper == 480 || paper == 380 || paper == 400 || paper == 200)
                    {
                        MessageBox.Show("There is a paper Jam! Please remove the Jam and then hit the ok button to continue!", "Important Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                    }
                    lblPaperAmount.Text = Convert.ToString(Convert.ToInt32(lblPaperAmount.Text) - 1);
                    lblTonerAmount.Text = Convert.ToString(Convert.ToInt32(lblTonerAmount.Text) - 1);
                    Thread.Sleep(1000);
                }
                txtMessage.Text = "Job is completed!";
            }

2 Answers2

2

Try adding a call to Refresh after setting the text. It's possible you're entering the loop quickly enough the refresh does not happen until you exit.

else
{
    txtMessage.Text = "Printing...";
    txtMessage.Refresh(); //force the control to redraw
    for (int i = numberCopies; i != 0; i--)
    {
        int paper = Convert.ToInt32(lblPaperAmount.Text);
        paper--;
        if (paper == 480 || paper == 380 || paper == 400 || paper == 200)
        {
            MessageBox.Show("There is a paper Jam! Please remove the Jam and then hit the ok button to continue!", "Important Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

        }
        lblPaperAmount.Text = Convert.ToString(Convert.ToInt32(lblPaperAmount.Text) - 1);
        lblTonerAmount.Text = Convert.ToString(Convert.ToInt32(lblTonerAmount.Text) - 1);
        Thread.Sleep(1000);
    }
    txtMessage.Text = "Job is completed!";
}
jac
  • 9,666
  • 2
  • 34
  • 63
  • You'll notice that your UI will still be frozen while "printing" though. Hit print and try to move or resize the form. – Blorgbeard Jul 24 '15 at 00:11
0

Your code is running in a single thread. You should set your text to "Printing..." then spin off a new thread to do your paper jam for loop.

Please see this question and the top 3 answers.

Community
  • 1
  • 1
Biff MaGriff
  • 8,102
  • 9
  • 61
  • 98