C# Thread 없이 Delay시키기
😀 Delay를 사용하게 된 계기
Thread.Sleep() 메서드로 일시 중단할 수 있지만,
Sleep()이 되는 동안 자유롭게 컨트롤이 불가하고
UI 화면에 보여지는 Delay를 주기 위해서 사용하기
위해서는 비효율적이라고 판단이 되어서이다.
😀 Delay 구현하기
private static DateTime Delay(int MS)
{
// Thread 와 Timer보다 효율 적으로 사용할 수 있음.
DateTime ThisMoment = DateTime.Now;
TimeSpan duration = new TimeSpan(0, 0, 0, 0, MS);
DateTime AfterWards = ThisMoment.Add(duration);
while (AfterWards >= ThisMoment)
{
System.Windows.Forms.Application.DoEvents();
ThisMoment = DateTime.Now;
}
return DateTime.Now;
}
😀 Delay 사용하기
private void test()
{
// 테스트
for (i = 0; i<100;i++)
{
Diagnostics.Debug.print(i);
Delay(500);
}
}
😀 Source Code
private void test()
{
// 테스트
for (i = 0; i<100;i++)
{
Diagnostics.Debug.print(i);
Delay(500);
}
}
private static DateTime Delay(int MS)
{
// Thread 와 Timer보다 효율 적으로 사용할 수 있음.
DateTime ThisMoment = DateTime.Now;
TimeSpan duration = new TimeSpan(0, 0, 0, 0, MS);
DateTime AfterWards = ThisMoment.Add(duration);
while (AfterWards >= ThisMoment)
{
System.Windows.Forms.Application.DoEvents();
ThisMoment = DateTime.Now;
}
return DateTime.Now;
}
댓글남기기