单击暂停按钮时如何暂停循环?

时间:2015-07-24 00:46:17

标签: c# .net

当我运行程序并尝试单击暂停按钮时,没有任何反应。我不确定如何让它完全正常工作。我有一个名为pause的bool变量,pause设置为false。单击暂停按钮后,应将该变量设置为true。然后循环检查它并应该向用户显示一条消息。非常感谢任何帮助!

namespace Practice2
 {
   public partial class Form1 : Form
  {
    photocopier printer = new photocopier(500, 2500);
    bool pause = false;
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void btnStart_Click(object sender, EventArgs e)
    {

        if (checkText(txtNumberCopies.Text) == true)
        {
            int numberCopies = Convert.ToInt32(txtNumberCopies.Text);
            int toner = Convert.ToInt32(lblTonerAmount.Text);
            int paperCapacity = Convert.ToInt32(lblPaperAmount.Text);

            if (toner <= 625 && paperCapacity <= 125)
            {
                txtMessage.Text = "Printer is low on Toner and Paper!";
            }
            else if (toner <= 625){
                txtMessage.Text = "Printer Toner is low!";
            }
            else if (paperCapacity <= 125)
            {
                txtMessage.Text = "Printer Paper is low!";
            }
            else
            {
                txtMessage.Text = "Printing...";
                txtMessage.Refresh();
                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);

                    }

                    if (pause == true)
                    {
                        MessageBox.Show("Press the ok button when ready 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!";
            }

        }
    }

    private void btnAddPaper_Click(object sender, EventArgs e)
    {
        int paperAmount = Convert.ToInt32(lblPaperAmount.Text);
        if (checkText(txtAddPaper.Text) == true && paperAmount <= 500)
        {
            lblPaperAmount.Text = Convert.ToString(paperAmount + Convert.ToInt32(txtAddPaper.Text));
        }
        else
        {
            txtMessage.Text = "Printer paper is at capacity!";
        }
    }

    private bool checkText(string textBox)
    {
        if (textBox.Equals("") || textBox == null)
        {
            txtMessage.Text = "Please enter a value in the text box!";
            return false;
        }
        return true;
    }

    private void btnReplaceToner_Click(object sender, EventArgs e)
    {
        lblTonerAmount.Text = Convert.ToString(printer.Toner);
    }

    private void btnPauseCancel_Click(object sender, EventArgs e)
    {
        pause = true;

    }
}
}

2 个答案:

答案 0 :(得分:2)

问题在于你正在UI线程上进行工作,因此UI线程很忙并且无法处理消息(例如按钮点击)。例如,您需要使用BackgroundWorkerTask.Run来处理工作线程。

答案 1 :(得分:1)

for循环在UI线程上,所以当for循环运行时,你无法对UI做任何事情。我建议您使用System.Windows.Forms.Timer来完成这项工作。您可以将间隔设置为1并且运行速度非常快,但不会像for循环那样快。但是间隔= 1对你来说已经足够了。

让我告诉你:

Timer timer = new Timer () {Interval=1};

创建一个新的计时器对象。 输入

timer.Tick +=
在构造函数中

并按两次TAB,这应生成一个事件处理程序。在事件处理程序中写下你想要做的事情。 呼叫timer.Stop暂停计时器,timer.Start启动计时器。