Auto Shut-Down to Windows Forms App After N Minutes of Inactivity

J

Jeff S

I'm creating a new Windows Forms MDI application...

How can I add cause the application to shut itself down after a period of
[no user activity]?

I did this in an old COM/VB6 application and I had to have code behind
practically all visible UI controls (triggered on the Mouse_Move event) that
reset a global variable monitored by a timer. If the global value
incremented past a certain point, then a Timer event procedure would cause a
dialog to appear. If the user failed to respond to the dialog after 10
seconds, then the app would shut down.

Does .NET give us an easier way to accomplish the same? I don't want to have
to detect the mouse_move event of practically every UI control and have a
timer running in the background the whole time, if possible.

Thanks!
 
J

Jeff S

Hummm, MousePosition is a property. How would I use that to determine if
there has been any user activity? Are you suggesting that I periodically
check the mouse coordinates? Not sure that would work for all scenarios -
like if the user is simply typing and not moving the mouse, or if they have
multiple MDI child windows open, then I'd have to have complicated logic
that determines which is the active one... But it might be part of a
solution...

-Jeff


Jared said:
Hi,

I would try to use
system.Windows.Forms.Form.ActiveForm.MousePosition




Jeff S said:
I'm creating a new Windows Forms MDI application...

How can I add cause the application to shut itself down after a period of
[no user activity]?

I did this in an old COM/VB6 application and I had to have code behind
practically all visible UI controls (triggered on the Mouse_Move event)
that reset a global variable monitored by a timer. If the global value
incremented past a certain point, then a Timer event procedure would
cause a dialog to appear. If the user failed to respond to the dialog
after 10 seconds, then the app would shut down.

Does .NET give us an easier way to accomplish the same? I don't want to
have to detect the mouse_move event of practically every UI control and
have a timer running in the background the whole time, if possible.

Thanks!
 
J

Jared

As for key events, MDIParent.KeyPreview = true

Not sure if this would have to be done for all forms?

Yes, I was thinking of using a timer.


Jeff S said:
Hummm, MousePosition is a property. How would I use that to determine if
there has been any user activity? Are you suggesting that I periodically
check the mouse coordinates? Not sure that would work for all scenarios -
like if the user is simply typing and not moving the mouse, or if they
have multiple MDI child windows open, then I'd have to have complicated
logic that determines which is the active one... But it might be part of a
solution...

-Jeff


Jared said:
Hi,

I would try to use
system.Windows.Forms.Form.ActiveForm.MousePosition




Jeff S said:
I'm creating a new Windows Forms MDI application...

How can I add cause the application to shut itself down after a period
of [no user activity]?

I did this in an old COM/VB6 application and I had to have code behind
practically all visible UI controls (triggered on the Mouse_Move event)
that reset a global variable monitored by a timer. If the global value
incremented past a certain point, then a Timer event procedure would
cause a dialog to appear. If the user failed to respond to the dialog
after 10 seconds, then the app would shut down.

Does .NET give us an easier way to accomplish the same? I don't want to
have to detect the mouse_move event of practically every UI control and
have a timer running in the background the whole time, if possible.

Thanks!
 
S

Stoitcho Goutsev \(100\)

Jared,

First off mouse and key messages comes through the windows message loop. In
this case you can use message filter to handle all mouse and keyboard
message globally for the whole application. Look at
Application.AddMessageFilter.

Secondly it depends on your definition on user inactivity. Is it just not
working wiht the application or is just not using that particular
application? In the latter case you can use message filters as I suggested
earlier in the former you have no managed solution. You should resort do
native API GetLastInputInfo (min req Win2K)

Third option is to use low level keyboard and mouse hookd which requirese
PInvoke again.

--
HTH
Stoitcho Goutsev (100)


Jared said:
As for key events, MDIParent.KeyPreview = true

Not sure if this would have to be done for all forms?

Yes, I was thinking of using a timer.


Jeff S said:
Hummm, MousePosition is a property. How would I use that to determine if
there has been any user activity? Are you suggesting that I periodically
check the mouse coordinates? Not sure that would work for all scenarios -
like if the user is simply typing and not moving the mouse, or if they
have multiple MDI child windows open, then I'd have to have complicated
logic that determines which is the active one... But it might be part of
a solution...

-Jeff


Jared said:
Hi,

I would try to use
system.Windows.Forms.Form.ActiveForm.MousePosition




I'm creating a new Windows Forms MDI application...

How can I add cause the application to shut itself down after a period
of [no user activity]?

I did this in an old COM/VB6 application and I had to have code behind
practically all visible UI controls (triggered on the Mouse_Move event)
that reset a global variable monitored by a timer. If the global value
incremented past a certain point, then a Timer event procedure would
cause a dialog to appear. If the user failed to respond to the dialog
after 10 seconds, then the app would shut down.

Does .NET give us an easier way to accomplish the same? I don't want to
have to detect the mouse_move event of practically every UI control and
have a timer running in the background the whole time, if possible.

Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top