简体   繁体   中英

Why won't the printing message print in my C# code?

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!";
            }

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!";
}

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.

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