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

本文关键字:暂停 按钮 循环 单击 | 更新日期: 2023-09-27 18:04:28

当我运行程序并尝试单击暂停按钮时,什么也没有发生。我不确定我怎么能得到这个工作完全。我有一个bool变量叫做pause, 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;
    }
}
}

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

问题是你正在UI线程上做工作,所以UI线程很忙,不能处理消息(例如按钮点击)。您需要在工作线程上完成工作,例如使用BackgroundWorkerTask.Run

for循环在UI线程上,所以当for循环运行时,你不能对UI做任何事情。我建议你用System.Windows.Forms.Timer来做这项工作。您将间隔设置为1,这将运行得非常快,但不如for循环快。但是interval = 1就足够了。

让我展示给你看:

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

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

timer.Tick +=

中的构造函数并按两次TAB键,这应该会生成一个事件处理程序。编写要在事件处理程序中执行的操作。呼叫timer.Stop暂停定时器,呼叫timer.Start启动定时器