为什么不在我的C#代码中打印打印消息?

时间:2015-07-23 23:10:19

标签: c# for-loop

我正在尝试打印消息"打印..."在循环运行之前到txtMessage.Text文本框但在循环运行之前它永远不会打印到文本框。知道为什么吗?

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 个答案:

答案 0 :(得分:2)

尝试在设置文本后添加对Refresh的调用。您可能会快速进入循环,直到退出时才会进行刷新。

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

答案 1 :(得分:0)

您的代码在单个线程中运行。您应该将文本设置为“正在打印...”然后旋转一个新线程以进行卡纸循环。

请参阅this问题和前3个答案。