2016-08-02 123 views
-4

我在文本框中有一些價值。我想在文本框中計算該值的時間。表示該值在文本框中存在多長時間。計算文本框中值的總時間。我想計算值的時間

值是布爾類型。它可以是1或者0。我想計算每個值的時間跨度也是它們的區別。

+0

好,你想這樣做。到目前爲止你做了什麼? –

+0

沒有什麼兄弟這就是爲什麼我在這裏.thats最後一件事remaning .task給我 –

+0

人們傾向於無反應和downvotes當一個問題是沒有任何可見的努力來解決這個問題,當問題與措詞「I想要「或」我需要「。 –

回答

0

你發佈的代碼並不多,但我會試試看。 我在代碼中看不到任何bool變量。 你可能應該有一個你可以保存當前狀態的地方。

,因爲你寫的值轉換爲TextBox,你可以啓動MMbach行之後提出的計時器:

sqlserver_status.Text = "Active"; 
// start timer here 

只要進一步在你的代碼這一狀態的變化,你會停止計時器並檢查經過時間。

您也可以使用StopWatch類。 它有一個屬性Elapsed其中:

獲取當前實例測量的總的經過時間。

如果你需要在後臺運行它,我會建議去Timer

下面是一個用System.Diagnostics.Stopwatch實現的小Demo。 對這個問題有更多的認識。它總是取決於你的程序的結構,哪種實現更好或更糟。

這是一個小型控制檯應用程序,您在此決定要更改State變量。它會監控你的決策過程。

public class TimeDemo 
{ 
    // Property to catch the timespan 
    public TimeSpan TimeOfState { get; set; } 

    // Full Property for the state 
    private bool state; 

    public bool State 
    { 
     get { return state; } 
     set 
     { 
      // whenever a new state value is set start measuring 
      state = value; 
      this.TimeOfState = StopTime(); 
     } 
    } 
    // Use this to stop the time 
    public System.Diagnostics.Stopwatch StopWatch { get; set; } 

    public TimeDemo() 
    { 
     this.StopWatch = new System.Diagnostics.Stopwatch(); 
    } 
    //Method to measure the elapsed time 
    public TimeSpan StopTime() 
    { 
     TimeSpan t = new TimeSpan(0, 0, 0); 

     if (this.StopWatch.IsRunning) 
     { 
      this.StopWatch.Stop(); 
      t = this.StopWatch.Elapsed; 
      this.StopWatch.Restart(); 
      return t; 
     } 
     else 
     { 
      this.StopWatch.Start(); 
      return t; 
     } 
    } 

    public void Demo() 
    { 
     Console.WriteLine("Please press Enter whenever you want.."); 
     Console.ReadKey(); 
     this.State = !this.State; 

     Console.WriteLine("Elapsed Time: " + TimeOfState.ToString()); 


     Console.WriteLine("Please press Enter whenever you want.."); 
     Console.ReadKey(); 
     this.State = !this.State; 

     Console.WriteLine("Elapsed Time: " + TimeOfState.ToString()); 



     Console.WriteLine("Please press Enter whenever you want.."); 
     Console.ReadKey(); 
     this.State = !this.State; 

     Console.WriteLine("Elapsed Time: " + TimeOfState.ToString()); 


    } 
} 

也許你可以根據你的情況進行調整。

+0

您能否爲我提供演示請 –

+0

@umarabbasi我編輯了我的答案。希望它可以幫助 –

+0

謝謝它幫助很多 –