2016-05-12 26 views
-2

入力した時間(時、分、秒)の後にコンピュータをシャットダウンするプログラムを作成しようとしています。 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++; 
     } 
    } 
} 
+1

UIスレッドをブロックしないでください。 – SLaks

+0

新しいテキストがいつ表示されますか?眠った直後、はい?それはいつですか?リフレッシュする時間をrzbでしたか?またはそれを強制する? – TaW

+0

BTW:UIスレッドをブロックすると、UIの更新をブロックするだけでなく、System.Windows.Forms.Timerもブロックします:) – Eser

答えて

1

SLaksはあなたがブロックした場合にUIボックスをスレッド、言われたように更新されることはありませんが、.NETでは、あなたは/非同期を待っています。

はまた、あなたがカウントダウンをしたい場合はこれが

TimeSpan time = TimeSpan.FromSeconds(totalSeconds - ticks);  
richTextBox1.AppendText(String.Format(timeOutput)); 

でなければなりません

TimeSpan time = TimeSpan.FromSeconds(totalSeconds); 
richTextBox1.AppendText(String.Format(timeOutput)); 

とボックスの内容を設定します。

+0

これはうまくいきませんでした:( – Conor

+0

@Conorこのようなコメントは嫌いですが、「これはうまくいかない」とはどういう意味ですか?コンパイル時にエラーが発生した場合、例外がありましたらどうしたらいいですか? – Eser

+0

他の時間は*が動作しないことを伝えます*;) – Gusman

関連する問題