회사_ C#, MSSQL, WPF
WPF C# DispatcherTimer 란? | 화면 갱신 방법 | 화면 업데이트
lsme
2024. 7. 7. 09:23
1초마다 계속해서 업데이트(화면 갱신)가 되어야하는 화면일 경우에는
DispatcherTimer 클래스를 사용해서 기능을 만들 수 있다.
나는 popup (버튼 클릭시 나오는 화면)에서 타이머를 설정하고 싶다.
[공식 글]
//DispatcherTimer setup
dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = TimeSpan.FromSeconds(1);
dispatcherTimer.Start();
[적용 코드]
public 클래스명()
{
InitializeComponent();
this.Unloaded += WdViewStationStatus_Unloaded;
}
private void WdViewStationStatus_Unloaded(object sender, RoutedEventArgs e)
{
try
{
if (this._Timer != null) this._Timer.Stop();
}
catch (Exception ex)
{
this._Log.SetHisotry(clsHistory.eHistoryType.Error, ex.ToString());
}
}
private DispatcherTimer _Timer;
public void StartProcess()
{
try
{
this.UpdateDisplay();
this._Timer = new DispatcherTimer();
this._Timer.Interval = new TimeSpan(0, 0, 1); //1초마다
this._Timer.Tick += _Timer_Tick;
this._Timer.Start();
this.Topmost = true;
this.ShowDialog();
}
catch(Exception ex)
{
this._Log.SetHisotry(clsHistory.eHistoryType.Error, ex.ToString());
}
}
private void _Timer_Tick(object? sender, EventArgs e)
{
try
{
this.UpdateDisplay();
}
catch (Exception ex)
{
this._Log.SetHisotry(clsHistory.eHistoryType.Error, ex.ToString());
}
}
this.Unloaded += WdViewStationStatus_Unloaded; 를 해줘야 popup 종료시 타이머가 꺼지는 것이다.
DispatcherTimer 클래스 (System.Windows.Threading)
지정된 시간 간격과 우선 순위로 처리되는 Dispatcher 큐로 통합되는 타이머입니다.
learn.microsoft.com