private void button1_Click(object sender, EventArgs e)
{
int minutes = Convert.ToInt32(numericUpDown1.Value);
if (minutes > 0)
{
int secs = minutes * 60;
progressBar1.Maximum = secs;
timer1.Enabled = true;
stopwatch.Start();
timer1.Start();
if (stopwatch.Elapsed.Minutes==numericUpDown1.Value)
{
Process.Start("shutdown", "/s /t 0");
stopwatch.Stop();
timer1.Stop();
}
}
else
{
MessageBox.Show("Please enter the correct minutes");
numericUpDown1.Value = 0;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
progressBar1.Increment(1);
}
似乎无法识别numbericUpDown的值。
答案 0 :(得分:1)
您的代码建议中有一些错误:
int secs = minutes * 60;
progressBar1.Maximum = secs;
timer1.Enabled = true;
stopwatch.Start();
timer1.Start();
if (stopwatch.Elapsed.Minutes==numericUpDown1.Value)
{
Process.Start("shutdown", "/s /t 0");
stopwatch.Stop();
timer1.Stop();
}
如果你去那里会怎么样?
你开始你的秒表和你的计时器。但没有什么能等你的计时器。因此,检查(stopwatch.Elapsed.Minutes==numericUpDown1.Value)
,它将在您启动代码后几毫秒内执行 - 您的程序将不再检查该语句,直到您再次按下button
,因此永远不会关闭,因为如果您单击按钮,则会再次将时间设置为新值。
bool goon = true;
while(goon)
{
//... check your time here
// if true goon = false;
}