Connecting Tech Pros Worldwide Help | Site Map

How to detect application idle time.

 
LinkBack Thread Tools Search this Thread
  #1  
Old November 16th, 2005, 01:41 PM
Frank Rizzo
Guest
 
Posts: n/a
Default 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.

  #2  
Old November 16th, 2005, 01:41 PM
e-lores
Guest
 
Posts: n/a
Default Re: How to detect application idle time.

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" <nospam@nospam.com> escribió en el mensaje
news:ucSjt6vwEHA.2600@TK2MSFTNGP09.phx.gbl...[color=blue]
> 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.[/color]


  #3  
Old November 16th, 2005, 01:42 PM
cody
Guest
 
Posts: n/a
Default Re: How to detect application idle time.

Use static event Application.Idle

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
"Frank Rizzo" <nospam@nospam.com> schrieb im Newsbeitrag
news:ucSjt6vwEHA.2600@TK2MSFTNGP09.phx.gbl...[color=blue]
> 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.[/color]


  #4  
Old November 16th, 2005, 01:43 PM
Frank Rizzo
Guest
 
Posts: n/a
Default Re: How to detect application idle time.

cody wrote:[color=blue]
> Use static event Application.Idle[/color]

I tried, but it seems to fire all the time (even when a timer event is
processed) and the arguments are empty.
  #5  
Old November 16th, 2005, 01:43 PM
ShaneB
Guest
 
Posts: n/a
Default Re: How to detect application idle time.

How are you coming to the conclusion that it is firing too much?

ShaneB

"Frank Rizzo" <nospam@nospam.com> wrote in message
news:%23IM%23i80wEHA.3840@tk2msftngp13.phx.gbl...[color=blue]
> cody wrote:[color=green]
>> Use static event Application.Idle[/color]
>
> I tried, but it seems to fire all the time (even when a timer event is
> processed) and the arguments are empty.[/color]


  #6  
Old November 16th, 2005, 01:43 PM
Frank Rizzo
Guest
 
Posts: n/a
Default Re: How to detect application idle time.

ShaneB wrote:[color=blue]
> How are you coming to the conclusion that it is firing too much?[/color]

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.

[color=blue]
>
> ShaneB
>
> "Frank Rizzo" <nospam@nospam.com> wrote in message
> news:%23IM%23i80wEHA.3840@tk2msftngp13.phx.gbl...
>[color=green]
>>cody wrote:
>>[color=darkred]
>>>Use static event Application.Idle[/color]
>>
>>I tried, but it seems to fire all the time (even when a timer event is
>>processed) and the arguments are empty.[/color]
>
>
>[/color]
  #7  
Old November 16th, 2005, 01:44 PM
ShaneB
Guest
 
Posts: n/a
Default Re: How to detect application idle time.

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" <nospam@nospam.com> wrote in message
news:eT6tX81wEHA.3808@TK2MSFTNGP15.phx.gbl...[color=blue]
> ShaneB wrote:[color=green]
>> How are you coming to the conclusion that it is firing too much?[/color]
>
> 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.
>
>[color=green]
>>
>> ShaneB
>>
>> "Frank Rizzo" <nospam@nospam.com> wrote in message
>> news:%23IM%23i80wEHA.3840@tk2msftngp13.phx.gbl...
>>[color=darkred]
>>>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.[/color]
>>
>>[/color][/color]

  #8  
Old November 16th, 2005, 01:45 PM
Justin Rogers
Guest
 
Posts: n/a
Default Re: How to detect application idle time.

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" <stormfire1@yahoo.com> wrote in message
news:unoChr3wEHA.3976@TK2MSFTNGP09.phx.gbl...[color=blue]
> 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" <nospam@nospam.com> wrote in message
> news:eT6tX81wEHA.3808@TK2MSFTNGP15.phx.gbl...[color=green]
>> ShaneB wrote:[color=darkred]
>>> How are you coming to the conclusion that it is firing too much?[/color]
>>
>> 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.
>>
>>[color=darkred]
>>>
>>> ShaneB
>>>
>>> "Frank Rizzo" <nospam@nospam.com> wrote in message
>>> news:%23IM%23i80wEHA.3840@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.
>>>
>>>[/color][/color]
>[/color]


  #9  
Old November 16th, 2005, 01:45 PM
ShaneB
Guest
 
Posts: n/a
Default Re: How to detect application idle time.

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;
}
}
}


  #10  
Old November 16th, 2005, 01:46 PM
Ravichandran J.V.
Guest
 
Posts: n/a
Default Re: How to detect application idle time.

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!
  #11  
Old November 16th, 2005, 01:46 PM
ShaneB
Guest
 
Posts: n/a
Default Re: How to detect application idle time.

Can you elaborate on this?

ShaneB


"Ravichandran J.V." <jvravichandran@yahoo.com> wrote in message
news:ewTk1mBxEHA.3612@tk2msftngp13.phx.gbl...[color=blue]
> 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![/color]


  #12  
Old November 16th, 2005, 01:48 PM
Frank Rizzo
Guest
 
Posts: n/a
Default Re: How to detect application idle time.

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:[color=blue]
> 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;
> }
> }
> }
>
>[/color]
  #13  
Old November 16th, 2005, 01:48 PM
ShaneB
Guest
 
Posts: n/a
Default Re: How to detect application idle time.

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" <nospam@nospam.com> wrote in message
news:uRfh3%23QxEHA.3624@TK2MSFTNGP09.phx.gbl...[color=blue]
> 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?[/color]


  #14  
Old November 16th, 2005, 01:51 PM
Willy Denoyette [MVP]
Guest
 
Posts: n/a
Default Re: How to detect application idle time.

Maybe you should use a non "windows message" based timer like
System.Timers.Timers or System.Threading.Timer.

Willy.

"Frank Rizzo" <nospam@nospam.com> wrote in message
news:uRfh3%23QxEHA.3624@TK2MSFTNGP09.phx.gbl...[color=blue]
> 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:[color=green]
>> 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;
>> }
>> }
>> }
>>[/color][/color]

  #15  
Old November 16th, 2005, 01:51 PM
ShaneB
Guest
 
Posts: n/a
Default Re: How to detect application idle time.

Good point. I would pursue using one of these two timers instead.

ShaneB

"Willy Denoyette [MVP]" <willy.denoyette@pandora.be> wrote in message
news:uaNskkbxEHA.1400@TK2MSFTNGP11.phx.gbl...[color=blue]
> Maybe you should use a non "windows message" based timer like
> System.Timers.Timers or System.Threading.Timer.
>
> Willy.[/color]


  #16  
Old November 16th, 2005, 01:53 PM
Ravichandran J.V.
Guest
 
Posts: n/a
Default Re: How to detect application idle time.

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!
  #17  
Old November 16th, 2005, 01:56 PM
ShaneB
Guest
 
Posts: n/a
Default Re: How to detect application idle time.

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." <jvravichandran@yahoo.com> wrote in message
news:On1EzTjxEHA.1524@TK2MSFTNGP09.phx.gbl...[color=blue]
> 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[/color]


  #18  
Old November 16th, 2005, 01:56 PM
ShaneB
Guest
 
Posts: n/a
Default Re: How to detect application idle time.

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." <jvravichandran@yahoo.com> wrote in message
news:On1EzTjxEHA.1524@TK2MSFTNGP09.phx.gbl...[color=blue]
> 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[/color]


  #19  
Old November 16th, 2005, 01:58 PM
Ravichandran J.V.
Guest
 
Posts: n/a
Default Re: How to detect application idle time.

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!
  #20  
Old November 16th, 2005, 01:58 PM
Ravichandran J.V.
Guest
 
Posts: n/a
Default Re: How to detect application idle time.

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!
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.