计算机未关闭并显示此代码(升 C)

Computer does not shut down with this code (C sharp)

    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 的值。

你的代码有一些错误建议:

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