473,396 Members | 1,886 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to detect application idle time.

I want to log the user out when there has been a period of inactivity in
the application. The key here is inactivity of the application, not the
system. I know that you can retrieve the inactivity of the system via
GetLastInputInfo. That's not what I want - I want inactivity of the
application.

Thanks.
Nov 16 '05 #1
19 46523
You can use a global Timer in your application and restart it when the user
takes any action. You must be sure that any action of the user will restart
the Timer. If the Timer_Tick events goes, you know the application
inactivity time.
It's only a suggestion.

Ernesto Lores
"Frank Rizzo" <no****@nospam.com> escribió en el mensaje
news:uc**************@TK2MSFTNGP09.phx.gbl...
I want to log the user out when there has been a period of inactivity in
the application. The key here is inactivity of the application, not the
system. I know that you can retrieve the inactivity of the system via
GetLastInputInfo. That's not what I want - I want inactivity of the
application.

Thanks.

Nov 16 '05 #2
Use static event Application.Idle

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
"Frank Rizzo" <no****@nospam.com> schrieb im Newsbeitrag
news:uc**************@TK2MSFTNGP09.phx.gbl...
I want to log the user out when there has been a period of inactivity in
the application. The key here is inactivity of the application, not the
system. I know that you can retrieve the inactivity of the system via
GetLastInputInfo. That's not what I want - I want inactivity of the
application.

Thanks.

Nov 16 '05 #3
cody wrote:
Use static event Application.Idle


I tried, but it seems to fire all the time (even when a timer event is
processed) and the arguments are empty.
Nov 16 '05 #4
How are you coming to the conclusion that it is firing too much?

ShaneB

"Frank Rizzo" <no****@nospam.com> wrote in message
news:%2******************@tk2msftngp13.phx.gbl...
cody wrote:
Use static event Application.Idle


I tried, but it seems to fire all the time (even when a timer event is
processed) and the arguments are empty.

Nov 16 '05 #5
ShaneB wrote:
How are you coming to the conclusion that it is firing too much?
I write to the log everytime it fires. So everytime, I move a mouse or
use a keyboard or a timer event fires, the Idle event goes off. I could
live with it, however, I have a bunch of timers in the application.


ShaneB

"Frank Rizzo" <no****@nospam.com> wrote in message
news:%2******************@tk2msftngp13.phx.gbl...
cody wrote:
Use static event Application.Idle


I tried, but it seems to fire all the time (even when a timer event is
processed) and the arguments are empty.


Nov 16 '05 #6
This is normal behaviour for the Application.Idle event. As soon as the
application's message queue is empty, the event is fired. This means that
after each mouse movement, timer event, keypress, etc...you get this event.
That's because the message queue got a message in it, processed it, and now
it is empty again. Remember, most applications spend 99% of their time in
an idle state...waiting on us. You will get tons of these messages during
the life of your application

What exactly are you trying to do at idle?

ShaneB

"Frank Rizzo" <no****@nospam.com> wrote in message
news:eT**************@TK2MSFTNGP15.phx.gbl...
ShaneB wrote:
How are you coming to the conclusion that it is firing too much?


I write to the log everytime it fires. So everytime, I move a mouse or
use a keyboard or a timer event fires, the Idle event goes off. I could
live with it, however, I have a bunch of timers in the application.


ShaneB

"Frank Rizzo" <no****@nospam.com> wrote in message
news:%2******************@tk2msftngp13.phx.gbl...
cody wrote:

Use static event Application.Idle

I tried, but it seems to fire all the time (even when a timer event is
processed) and the arguments are empty.



Nov 16 '05 #7
Apparently you need to merge the two suggestions on the table. First, since
Idle fires when your message queue is exhausted, then you should hook that
because it identifies the moment immediately following all user input having
been processed... In the idle event, reset your timer so that it restarts. If
your
timer fires an event, then you can assume the user has been idle for some
period of time.

You have to be careful with this, since exhausting the message queue is
required. You can hook into an LRO or long running operation that allows
your timer to expire. Another option is to hook up a message filter and
examine messages as they come through. This allows you to stop the timer
when you see an operation getting ready to start, followed by starting it
again when the idle event hits.

--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

"ShaneB" <st********@yahoo.com> wrote in message
news:un**************@TK2MSFTNGP09.phx.gbl...
This is normal behaviour for the Application.Idle event. As soon as the
application's message queue is empty, the event is fired. This means that
after each mouse movement, timer event, keypress, etc...you get this event.
That's because the message queue got a message in it, processed it, and now it
is empty again. Remember, most applications spend 99% of their time in an
idle state...waiting on us. You will get tons of these messages during the
life of your application

What exactly are you trying to do at idle?

ShaneB

"Frank Rizzo" <no****@nospam.com> wrote in message
news:eT**************@TK2MSFTNGP15.phx.gbl...
ShaneB wrote:
How are you coming to the conclusion that it is firing too much?


I write to the log everytime it fires. So everytime, I move a mouse or use a
keyboard or a timer event fires, the Idle event goes off. I could live with
it, however, I have a bunch of timers in the application.


ShaneB

"Frank Rizzo" <no****@nospam.com> wrote in message
news:%2******************@tk2msftngp13.phx.gbl...

cody wrote:

>Use static event Application.Idle

I tried, but it seems to fire all the time (even when a timer event is
processed) and the arguments are empty.

Nov 16 '05 #8
Here's one way to accomplish what you want. I'm be interested if someone
comes up with a better one. Application.Idle being a static event makes
this task a little more complicated than it had to be.

Anyway, you should be able to cut/paste the code below directly into an
empty project in place of the default Form1.cs and it should run fine.

ShaneB

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace MyApp
{
public class Form1 : System.Windows.Forms.Form, IMessageFilter //
note that we're implementing IMessageFilter because we want the see all
messages coming in.
{
private System.Windows.Forms.Timer timer1;
private System.ComponentModel.IContainer components;
private static int iEnteredIdleTicks; // we need a static
variable since Application.Idle is a static event.

public Form1()
{
InitializeComponent();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.timer1 = new
System.Windows.Forms.Timer(this.components);
this.timer1.Enabled = true; // the timer will always be
running
this.timer1.Interval = 60000; // we'll check every 60
seconds to see if the application has been idle too long
this.timer1.Tick += new
System.EventHandler(this.timer1_Tick_1);

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.Name = "Form1";
}

[STAThread]
static void Main()
{
Application.Idle += new EventHandler(Application_Idle); // we
want to process Idle events
Application.Run(new Form1());
}

private static void Application_Idle(object sender, EventArgs e)
{
iEnteredIdleTicks = Environment.TickCount;
}

private void timer1_Tick_1(object sender, System.EventArgs e)
{
if ( (iEnteredIdleTicks != 0) && (Environment.TickCount -
iEnteredIdleTicks > 300000) )
// In the above line, my test timeout value is 300 seconds (5
minutes). Note that depending on when the timer event is fired, this could
effectively be as long as 5 minutes + the timer1.Interval.
{
timer1.Enabled = false; // Prevents any further timer
messages...just in case.
// <you would log off the user here>
Application.Exit();
}
}

public bool PreFilterMessage(ref Message m)
{
iEnteredIdleTicks = 0; // reset our ticks since we got a new
message to handle...ie, we're no longer idling.
return false;
}
}
}
Nov 16 '05 #9
Use the Process class. You can check for an Idle property when you start
your app through the process 'p' object.

Process p=new Process();
with regards,
J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #10
Can you elaborate on this?

ShaneB
"Ravichandran J.V." <jv************@yahoo.com> wrote in message
news:ew**************@tk2msftngp13.phx.gbl...
Use the Process class. You can check for an Idle property when you start
your app through the process 'p' object.

Process p=new Process();
with regards,
J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #11
That's what I have, Shane. However, the problem is that the Timer event
itself causes the Idle event to be fired. How are you addressing that?

ShaneB wrote:
Here's one way to accomplish what you want. I'm be interested if someone
comes up with a better one. Application.Idle being a static event makes
this task a little more complicated than it had to be.

Anyway, you should be able to cut/paste the code below directly into an
empty project in place of the default Form1.cs and it should run fine.

ShaneB

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace MyApp
{
public class Form1 : System.Windows.Forms.Form, IMessageFilter //
note that we're implementing IMessageFilter because we want the see all
messages coming in.
{
private System.Windows.Forms.Timer timer1;
private System.ComponentModel.IContainer components;
private static int iEnteredIdleTicks; // we need a static
variable since Application.Idle is a static event.

public Form1()
{
InitializeComponent();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.timer1 = new
System.Windows.Forms.Timer(this.components);
this.timer1.Enabled = true; // the timer will always be
running
this.timer1.Interval = 60000; // we'll check every 60
seconds to see if the application has been idle too long
this.timer1.Tick += new
System.EventHandler(this.timer1_Tick_1);

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.Name = "Form1";
}

[STAThread]
static void Main()
{
Application.Idle += new EventHandler(Application_Idle); // we
want to process Idle events
Application.Run(new Form1());
}

private static void Application_Idle(object sender, EventArgs e)
{
iEnteredIdleTicks = Environment.TickCount;
}

private void timer1_Tick_1(object sender, System.EventArgs e)
{
if ( (iEnteredIdleTicks != 0) && (Environment.TickCount -
iEnteredIdleTicks > 300000) )
// In the above line, my test timeout value is 300 seconds (5
minutes). Note that depending on when the timer event is fired, this could
effectively be as long as 5 minutes + the timer1.Interval.
{
timer1.Enabled = false; // Prevents any further timer
messages...just in case.
// <you would log off the user here>
Application.Exit();
}
}

public bool PreFilterMessage(ref Message m)
{
iEnteredIdleTicks = 0; // reset our ticks since we got a new
message to handle...ie, we're no longer idling.
return false;
}
}
}

Nov 16 '05 #12
Ahh...excellent point. Change the following:

public bool PreFilterMessage(ref Message m)
{
const int WM_TIMER = 275;
if (m.Msg != WM_TIMER)
{
iEnteredIdleTicks = 0;
}
return false;
}

ShaneB

"Frank Rizzo" <no****@nospam.com> wrote in message
news:uR****************@TK2MSFTNGP09.phx.gbl...
That's what I have, Shane. However, the problem is that the Timer event
itself causes the Idle event to be fired. How are you addressing that?

Nov 16 '05 #13
Maybe you should use a non "windows message" based timer like
System.Timers.Timers or System.Threading.Timer.

Willy.

"Frank Rizzo" <no****@nospam.com> wrote in message
news:uR****************@TK2MSFTNGP09.phx.gbl...
That's what I have, Shane. However, the problem is that the Timer event
itself causes the Idle event to be fired. How are you addressing that?

ShaneB wrote:
Here's one way to accomplish what you want. I'm be interested if someone
comes up with a better one. Application.Idle being a static event makes
this task a little more complicated than it had to be.

Anyway, you should be able to cut/paste the code below directly into an
empty project in place of the default Form1.cs and it should run fine.

ShaneB

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace MyApp
{
public class Form1 : System.Windows.Forms.Form, IMessageFilter //
note that we're implementing IMessageFilter because we want the see all
messages coming in.
{
private System.Windows.Forms.Timer timer1;
private System.ComponentModel.IContainer components;
private static int iEnteredIdleTicks; // we need a static
variable since Application.Idle is a static event.

public Form1()
{
InitializeComponent();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.timer1 = new
System.Windows.Forms.Timer(this.components);
this.timer1.Enabled = true; // the timer will always
be running
this.timer1.Interval = 60000; // we'll check every 60
seconds to see if the application has been idle too long
this.timer1.Tick += new
System.EventHandler(this.timer1_Tick_1);

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.Name = "Form1";
}

[STAThread]
static void Main()
{
Application.Idle += new EventHandler(Application_Idle); //
we want to process Idle events
Application.Run(new Form1());
}

private static void Application_Idle(object sender, EventArgs e)
{
iEnteredIdleTicks = Environment.TickCount;
}

private void timer1_Tick_1(object sender, System.EventArgs e)
{
if ( (iEnteredIdleTicks != 0) && (Environment.TickCount -
iEnteredIdleTicks > 300000) )
// In the above line, my test timeout value is 300 seconds (5
minutes). Note that depending on when the timer event is fired, this
could effectively be as long as 5 minutes + the timer1.Interval.
{
timer1.Enabled = false; // Prevents any further timer
messages...just in case.
// <you would log off the user here>
Application.Exit();
}
}

public bool PreFilterMessage(ref Message m)
{
iEnteredIdleTicks = 0; // reset our ticks since we got a
new message to handle...ie, we're no longer idling.
return false;
}
}
}


Nov 16 '05 #14
Good point. I would pursue using one of these two timers instead.

ShaneB

"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:ua**************@TK2MSFTNGP11.phx.gbl...
Maybe you should use a non "windows message" based timer like
System.Timers.Timers or System.Threading.Timer.

Willy.

Nov 16 '05 #15
My keyboard is not working. SO, add the open close parentheses as
applicable.

using System.Diagnostics;
....
Process p=new Process);
ProcessStartInfo pf=new ProcessStartInfo"Application name");
p.StartInfo=pf;
p.Start;
if p.Responding
{
// the process is responding.
}

I hope the above solves your problem.

with regards,
J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #16
I believe the original poster is trying to detect idle time from within the
same application. Similar to how windows 2000/Xp/Nt locks the computer if
you're away for a too long (if you have it set up that way). Regardless,
the method below only indicates if the application is too busy to answer a
message or is locked up. It has nothing to do with whether it is idle or
not.

ShaneB

"Ravichandran J.V." <jv************@yahoo.com> wrote in message
news:On**************@TK2MSFTNGP09.phx.gbl...
My keyboard is not working. SO, add the open close parentheses as
applicable.

using System.Diagnostics;
...
Process p=new Process);
ProcessStartInfo pf=new ProcessStartInfo"Application name");
p.StartInfo=pf;
p.Start;
if p.Responding
{
// the process is responding.
}

I hope the above solves your problem.

with regards,
J.V.Ravichandran

Nov 16 '05 #17
I believe the original poster is trying to detect idle time from within the
same application. Similar to how windows 2000/Xp/Nt locks the computer if
you're away for a too long (if you have it set up that way). Regardless,
the method below only indicates if the application is too busy to answer a
message or is locked up. It has nothing to do with whether it is idle or
not.

ShaneB

"Ravichandran J.V." <jv************@yahoo.com> wrote in message
news:On**************@TK2MSFTNGP09.phx.gbl...
My keyboard is not working. SO, add the open close parentheses as
applicable.

using System.Diagnostics;
...
Process p=new Process);
ProcessStartInfo pf=new ProcessStartInfo"Application name");
p.StartInfo=pf;
p.Start;
if p.Responding
{
// the process is responding.
}

I hope the above solves your problem.

with regards,
J.V.Ravichandran

Nov 16 '05 #18
OOPS! I believe you are right. I must have read the OP rather too
quickly.

with regards,
J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #19
OOPS! I believe you are right. I must have read the OP rather too
quickly.

with regards,
J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dayne | last post by:
I am working on a vb.net project and would like to have a automatic timeout after a set idle time..Any ideas? Dayne
0
by: Hyper X | last post by:
Hey guys, I have about 150 forms using the ComboBoxes. There is at least one combobox in every form. I am filling the comboboxes on FormLoad of every form. Eg., Fill Staff table on form...
5
by: smahaboob | last post by:
Hi iam looking for applications idle time like word,excel,explorar or cpu idle time, not for calculating the creating application idle time. pls help me
0
by: Rick | last post by:
I need to publish a perf counter to monitor total application idle time. I can calculate the total idle time but I don't know how to publish it. Can anyone suggest which type of counter I should...
3
by: Peri | last post by:
Dear All, Can you please help me out in detecting the application idle time. I would like to lock the application if it is idle for more that 10 minutes. FYI - I have already tried...
1
by: Visu | last post by:
Hi All! How to find the application idle time in VB.NET windows application? We can get the windows Idle time Private Declare Function GetLastInputInfo Lib "user32.dll" But i want the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.