入力した時間(時、分、秒)の後にコンピュータをシャットダウンするプログラムを作成しようとしています。 richTextBox1はまったく更新されませんが、すべて動作します。誰かがこの問題を助けてくれますか?前もって感謝します。ちょうどあなたの方法はprivate async void button1_Click
にし、代わりにThread.Sleep
使用await Task.Delay
の変換、更新するrichTextBox1を取得できません
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Timers;
namespace ShutdownPC
{
public partial class Form1 : Form
{
public int inputHours = 0;
public int inputMinutes = 0;
public int inputSeconds = 0;
public int totalSeconds = 0;
public int totalMilliseconds = 0;
public int ticks = 0;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Enabled = false;
textBox2.Enabled = false;
textBox3.Enabled = false;
button1.Enabled = false;
// Hours
if (!string.IsNullOrWhiteSpace(textBox1.Text))
{
inputHours = int.Parse(textBox1.Text);
}
// Minutes
if (!string.IsNullOrWhiteSpace(textBox2.Text))
{
inputMinutes = int.Parse(textBox2.Text);
}
// Seconds
if (!string.IsNullOrWhiteSpace(textBox3.Text))
{
inputSeconds = int.Parse(textBox3.Text);
}
// Updating richTextBox1 every second with remaining time left
totalSeconds = (inputHours * 3600) + (inputMinutes * 60) + inputSeconds;
timer1.Start();
while (ticks < totalSeconds)
{
TimeSpan time = TimeSpan.FromSeconds(totalSeconds);
string timeOutput = time.ToString(@"hh\:mm\:ss");
richTextBox1.AppendText(String.Format(timeOutput));
Thread.Sleep(1000);
richTextBox1.Clear();
}
// Shutting down computer
totalMilliseconds = ((inputHours * 3600) + (inputMinutes * 60) + inputSeconds) * 1000;
Thread.Sleep(totalMilliseconds);
richTextBox1.AppendText("end");
//Process.Start("shutdown", "/s /t 0");
}
private void timer1_Tick(object sender, EventArgs e)
{
ticks++;
}
}
}
UIスレッドをブロックしないでください。 – SLaks
新しいテキストがいつ表示されますか?眠った直後、はい?それはいつですか?リフレッシュする時間をrzbでしたか?またはそれを強制する? – TaW
BTW:UIスレッドをブロックすると、UIの更新をブロックするだけでなく、System.Windows.Forms.Timerもブロックします:) – Eser