如果用户在一段时间内没有使用过它,我需要关闭我的应用程序。 我现在使用的方法在单个窗口上效果很好,但我似乎无法使其成为全局。 这是我要做的事现在:自动关闭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;
}
}
我的问题是:
是有可能使这件事情,而不是全球每一个窗口,我创造改写呢?
有没有更好的方法来做到这一点?
谢谢!