【发布时间】:2021-09-02 10:33:42
【问题描述】:
我正在尝试构建一个 Windows 窗体(在 C# 中),它有 2 个按钮 - 启动和停止,它看起来像这样:
(点击开始按钮,停止按钮出现,开始按钮消失,反之亦然)。
单击开始按钮时,我希望我的程序每 x 秒运行一次特定代码,直到单击停止按钮。
我的问题如下:假设运行此特定代码需要 30 秒,我希望这 30 秒计入我的 60 秒间隔。
到目前为止,我的代码所做的是:计数 60 秒,执行某些代码,计数 60 秒,执行某些代码,计数 60 秒等......
我想要它做的是:在开始计算 60 秒时运行某些代码,完成代码,继续计算剩余时间(在我的示例中为 30 秒),再次运行代码(继续计算!)等等...
此外,我的代码在单击停止按钮时不会立即停止,它会再运行代码 1-2 次然后停止。
这是我目前的代码(按钮 1- 开始,按钮 2- 停止):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
static bool exitFlag = false;
static System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
public Form1()
{
InitializeComponent();
}
private void agTextBox1_Load(object sender, EventArgs e) {}
private void Form1_Load(object sender, EventArgs e){}
private void textBox1_TextChanged(object sender, EventArgs e){}
private void button1_Click(object sender, EventArgs e)
{
button1.Visible = false;
button2.Visible = true;
myTimer.Tick += new EventHandler(TimerEventProcessor);
// Sets the timer interval to 60 seconds.
myTimer.Interval = 60000;
myTimer.Start();
// Runs the timer, and raises the event.
while (exitFlag == false)
{
// Processes all the events in the queue.
Application.DoEvents();
}
}
private void TimerEventProcessor(object sender, EventArgs e)
{
//do something that takes x<60 seconds
myTimer.Stop();
// checks whether to continue running the timer.
if (!button1.Visible)
{
// Restarts the timer and increments the counter.
myTimer.Enabled = true;
}
else
{
// Stops the timer.
exitFlag = true;
}
}
private void button2_Click(object sender, EventArgs e)
{
button2.Visible = true;
button1.Visible = true;
}
}
}
【问题讨论】: