StopWatch를 만들려면 Timers.Timer를 만듭니다. 난 Perientular 시간 후 표시 시간 이벤트를 처리 Timer.Elapsed 사용할 수 있습니다. 타이머 간격을 1로 지정하고 true로 설정할 수 있습니다. 나는 또한 AutoReset을 true로 만든다. 그러나 문제는 당신에게Timer.Elapsed 화재 이벤트는 한 번만, 나는 매초마다 그것을 원합니다
System.Timers.Timer StopWatchTimer = new System.Timers.Timer();
Stopwatch sw = new Stopwatch();
public void StopwatchStartBtn_Click(object sender, ImageClickEventArgs e)
{
StopWatchTimer.Start();
StopWatchTimer.Interval = 1;
StopWatchTimer.Enabled = true;
StopWatchTimer.AutoReset =true;
sw.Start();
StopWatchTimer.Elapsed += new System.Timers.ElapsedEventHandler(StopWatchTimer_Tick);
}
protected void StopWatchStopBtn_Click(object sender, ImageClickEventArgs e)
{
TextBoxStopWatch.Text = "00:00:000";
StopWatchTimer.Stop();
sw.Reset();
sw.Stop();
}
public void StopWatchTimer_Tick(object sender,EventArgs e)
{
TextBoxStopWatch.Text= Convert.ToString(sw.Elapsed);
}
감사 ... 이벤트는 발사됩니다 해우리 내가 성공을 얻을 모든 대안을 시도하지만 텍스트 상자마다 Second.I에 시간을 변경할 수 있습니다 만 once.How 텍스트 상자에 시간을 얻을 수있다 업데이트 : Visual Studio에서 새 웹 사이트를 만들어서 사용해보십시오.하지만 여전히 성공을 얻지는 못합니다. 문제가 있습니다. 지금 업데이트는 내가 줄 바꿈 점을 설정할 때입니다.
TextBoxStopWatch.Text= Convert.ToString(sw.Elapsed);
텍스트가 계속 변경되지만 TextBox에는 표시되지 않습니다. 희망을 이해할 수 있습니다.
코드를 실행할 때 InvalidOperationException이 발생합니다. 그 이유는 StopWatchTimer_Tick 메서드가 다른 스레드의 컨트롤에 액세스하려고하기 때문입니다. 나머지 코드는 무엇입니까? – NotMe
흥미롭게도, 컴파일하고 VS 외부에서 직접 실행하면 예상대로 작동합니다. 어떤 버전의 C#을 사용하고 있습니까? – NotMe
@Chris Lively .NET Framework 4.0 – Hiren