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
'회사_ C#, MSSQL, WPF' 카테고리의 다른 글
C# WPF 제대로된 변수명 인식 에러 (0) | 2024.07.09 |
---|---|
WPF C# DispatcherTimer 와 timer의 차이점 (0) | 2024.07.08 |
C# WPF 스크롤바 안되는 이유 - 레이아웃 요소 확인! (0) | 2024.07.06 |
[WPF 레이아웃] StackPanel과 Gird의 차이점 | Gird로 구역 나누기 (0) | 2024.07.05 |
C# 오류 _ 자간 다른 (0) | 2024.06.29 |