如果用戶在一段時間內沒有使用過它,我需要關閉我的應用程序。 我現在使用的方法在單個窗口上效果很好,但我似乎無法使其成爲全局。 這是我要做的事現在:自動關閉WPF應用程序
DispatcherTimer dt;
public Window3()
{
InitializeComponent();
//initialize the timer
dt = new DispatcherTimer();
dt.Interval = TimeSpan.FromSeconds(1);
dt.Start();
dt.Tick += new EventHandler(dt_Tick);
}
long ticks = 0;
void dt_Tick(object sender, EventArgs e)
{
ticks++;
//close the application if 10 seconds passed without an event
if (ticks > 10)
{
Close();
}
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
//Initialize a hook
((HwndSource)PresentationSource.FromVisual(this)).AddHook(myHook);
}
private IntPtr myHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
//reset counter
ticks = 0;
switch (msg)
{
// process messages here
default:
return IntPtr.Zero;
}
}
我的問題是:
是有可能使這件事情,而不是全球每一個窗口,我創造改寫呢?
有沒有更好的方法來做到這一點?
謝謝!