94 lines
2.7 KiB
C#
94 lines
2.7 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Reactive;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
using System.Windows.Threading;
|
|
|
|
namespace WeldingCommon.Popup
|
|
{
|
|
public partial class TimerMessageBox : Window
|
|
{
|
|
private TaskCompletionSource<bool> _tcs;
|
|
private DispatcherTimer _timer;
|
|
private bool _isResultSet = false;
|
|
private bool _timeoutResult = false;
|
|
private TimeSpan _remaining;
|
|
|
|
private TimerMessageBox(string title, string message, bool timeoutValue, int timeoutMsec = 5000)
|
|
{
|
|
InitializeComponent();
|
|
Title = title;
|
|
TxtMessage.Text = message;
|
|
_timeoutResult = timeoutValue;
|
|
|
|
_remaining = TimeSpan.FromMilliseconds(timeoutMsec);
|
|
TxtCountdown.Text = $"남은 시간: {_remaining.Seconds}초";
|
|
|
|
_tcs = new TaskCompletionSource<bool>();
|
|
|
|
_timer = new DispatcherTimer();
|
|
_timer.Interval = TimeSpan.FromSeconds(1);
|
|
_timer.Tick += OnTimeout;
|
|
}
|
|
|
|
private void OnTimeout(object sender, EventArgs e)
|
|
{
|
|
_remaining = _remaining.Subtract(TimeSpan.FromSeconds(1));
|
|
TxtCountdown.Text = $"남은 시간: {_remaining.Seconds}초";
|
|
|
|
if (_remaining.TotalSeconds <= 0) {
|
|
_timer.Stop();
|
|
CloseWithResult(_timeoutResult);
|
|
}
|
|
}
|
|
|
|
private void BtnYes_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
_timer.Stop();
|
|
CloseWithResult(true);
|
|
}
|
|
|
|
private void BtnNo_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
_timer.Stop();
|
|
CloseWithResult(false);
|
|
}
|
|
|
|
private void CloseWithResult(bool result)
|
|
{
|
|
if (_isResultSet) return;
|
|
|
|
_isResultSet = true;
|
|
_tcs.TrySetResult(result);
|
|
this.Close();
|
|
}
|
|
|
|
//ESC
|
|
protected override void OnClosed(EventArgs e)
|
|
{
|
|
base.OnClosed(e);
|
|
CloseWithResult(false);
|
|
}
|
|
|
|
public static Task<bool> ShowAsync(Window owner, string title, string message, bool timeoutValue, int timeoutMsec = 5000)
|
|
{
|
|
TimerMessageBox IsOpend = Application.Current.Windows.OfType<TimerMessageBox>().FirstOrDefault(w => w.Title == title);
|
|
if(IsOpend != null)
|
|
return Task.FromResult(false);
|
|
|
|
var dlg = new TimerMessageBox(title, message, timeoutValue, timeoutMsec)
|
|
{
|
|
Owner = owner
|
|
};
|
|
|
|
dlg.Loaded += (s, e) =>
|
|
{
|
|
dlg._timer.Start(); // 로드 완료 후에 타이머 시작
|
|
};
|
|
|
|
dlg.Show();
|
|
return dlg._tcs.Task;
|
|
}
|
|
}
|
|
} |