I have a need to shutdown my application if the user haven't used it for certain period of time. The method I use now works great on a single window but i can't seem to make it global. this is how i do it now:
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;
}
}
My questions are:
is it possible to make this thing global instead of rewriting it in every window i create?
is there a better way to do it?
Thanks!