2013-05-09 3 views
2

의 메시지 상자를 표시하십시오. 특정 시간 (예 : 9 시간)이 경과 한 경우 "9 시간이 경과되었습니다."라는 메시지가 표시됩니다. 내 코드는 다음과 같습니다특정 시간이 지나면 C#

public partial class Form1 : Form 
{ 
    Stopwatch stopWatch = new Stopwatch(); 

    public Form1() 
    { 
     InitializeComponent(); 
     stopWatch.Start(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     double sec = stopWatch.ElapsedMilliseconds/1000; 
     double min = sec/60; 
     double hour = min/60; 
     if (hour == 9.00D) 
     { 
      stopWatch.Stop(); 
      MessageBox.Show("passed: " + hour.ToString("0.00")); 
     } 
    } 
} 

하고 문제는 코드의이 부분을 작성하는 곳 난 모르겠입니다 :이 코드를 작성 그래서

if (hour == 9.00D) 
     { 
      stopWatch.Stop(); 
      MessageBox.Show("passed: " + hour.ToString("0.00")); 
     } 

를? 이 일을 더 잘 수행 할 수 있다면 보여주십시오.

+2

타이머 이벤트가 발생할 때 코드를 작성합니다. Timer 클래스의 설명서를 읽어보십시오. –

+1

타이머를 사용하는 방법을 읽어야합니다. http://stackoverflow.com/questions/11094008/how-to-use-timer-in-c-sharp –

+0

버튼을 클릭하지 않은 경우에도 팝업을 표시 하시겠습니까? 또는 버튼을 클릭 한 후에 만 ​​표시하고 싶습니까? – gwin003

답변

2

감사하지 않는 것은 double hours이 정확히 9.00 일 가능성이 매우 낮습니다. 왜 타이머를 한 번만, 9 시간 후에 원하는 타이머를 요청하십시오.

Timer timer; 
public Form1() 
{ 
    InitializeComponent(); 
    timer.Tick += timer_Tick; 
    timer.Interval = TimeSpan.FromHours(9).TotalMilliseconds; 
    timer.Start(); 
} 

void timer_Tick(object sender, EventArgs e) 
{ 
    timer.Stop(); 
    MessageBox.Show("9 hours passed"); 
} 
+0

감사합니다. – samuraisxmali

1

특정 기간이 지나면 특정 작업을 수행하려면 System.Forms.Timer를 사용해야합니다 (Windows 양식의 경우). Elapsed 이벤트를 사용하고 조건을 구현할 수 있습니다.

0

대신 타이머를 사용해보십시오. Example from here

Timer timer; 
public Form1() 
{ 
    InitializeComponent(); 
    timer.Tick += new EventHandler(timer_Tick); // when timer ticks, timer_Tick will be called 
    timer.Interval = (1000) * (10);    // Timer will tick every 10 seconds 
    timer.Enabled = true;      // Enable the timer 
    timer.Start();        // Start the timer 
} 

void timer_Tick(object sender, EventArgs e) 
{ 
    double sec = stopWatch.ElapsedMilliseconds/1000; 
    double min = sec/60; 
    double hour = min/60; 
    if (hour == 9.00D) 
    { 
     stopWatch.Stop(); 
     MessageBox.Show("passed: " + hour.ToString("0.00")); 
    } 
} 
0

사용 Timer는 :

타이머는 정기적으로 코드를 호출합니다. 몇 초 또는 몇 분마다 메서드가 실행됩니다. 이 기능은 진단과 마찬가지로 중요한 프로그램 의 상태를 모니터링하는 데 유용합니다. System.Timers 네임 스페이스 이 유용합니다.

이 참조 :

다른 사람에 의해 설명 된대로, 직접) Stopwatch.Elapsed에 의해 반환되는 시간 범위의의 property TotalHours을 (사용할 수있는 타이머를 사용하는 것 외에도
public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 

      stopWatch.Start(); 
      tm.Interval = 1000; 
      tm.Enabled = true; 
      tm.Tick += new EventHandler(tm_Tick); 
      tm.Start(); 
     } 

     void tm_Tick(object sender, EventArgs e) 
     { 
      double sec = stopWatch.ElapsedMilliseconds/1000; 
      double min = sec/60; 
      double hour = min/60; 
      if (hour == 9.00D) 
      { 
       stopWatch.Stop(); 
       MessageBox.Show("passed: " + hour.ToString("0.00")); 
      } 
     } 
    } 
3

:

 TimeSpan ts = stopWatch.Elapsed; 
     if (ts.TotalHours >= 9) 
     { 
      MessageBox.Show("passed: " + ts.TotalHours.ToString("0.00")); 
     } 
+0

+1은'> ='을 사용합니다. – weston

+0

이 코드는 좋지만이 코드를 어디에 두어야합니까? – samuraisxmali

+1

"sec", "min"및 "hour"를 수동으로 계산하는 이전 코드 대신 Tick() 이벤트에 넣습니다. * 당신이 원하는 전체 시간 간격으로 Interval()을 설정 한 weston의 코드를 사용하지 않았다고 가정합니다! =) –

관련 문제