GetIdleTime() - Help

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

What's wrong with this code?

[DllImport ("coredll.dll", SetLastError=false)]
public static extern int GetIdleTime();

[DllImport ("coredll.dll", SetLastError=false)]
public static extern int GetTickCount();


try
{
int dwStartTick = GetTickCount();
int dwIdleSt = GetIdleTime();

Application.DoEvents();

int dwStopTick = GetTickCount();
int dwIdleEd = GetIdleTime();
int PercentIdle = ((100*(dwIdleEd - dwIdleSt)) / (dwStopTick -
dwStartTick));

MessageBox.Show(PercentIdle.ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

the main body of the code in a timer which fires every 20 seconds.

The messagebox shows "0" the first time it runs then i get a divde by zero
error after that.

any ideas?

I need to give focus back to my application if the pda is idle for longer
than 30 seconds.

Thanks guys.
 
Who says that the operation is going to take more than 1ms? If not, you've
got a rather obvious divide by zero case when dwStopTick = dwStartTick. I
don't see anything wrong with the declarations...

Paul T.
 
For starters, GetTickCount does not give you a very precise interval
measurement. You would be better off using QueryPerformanceCounters. Because
of its coarsness, it is likely to return the same value before and after
DoEvents resulting in divide by zero condition. You should alway compare the
start and stop time and ignore measurement interval if they are the same.

I realize that the code you use is borrowed straight from the GetIdleTime
function documentation, but the sample code on MSDN has its share of
mistakes.
 
Right ok....

Is there anyway one of you two can give me a small example of this working,
i'm kind of new to this PDA stuff and i'm running a bit short of time..

All i need is to give focus back to my application if the pda is idle for
more than 30 seconds.

Thanks, really appreciate it.

Alex Feinman said:
For starters, GetTickCount does not give you a very precise interval
measurement. You would be better off using QueryPerformanceCounters. Because
of its coarsness, it is likely to return the same value before and after
DoEvents resulting in divide by zero condition. You should alway compare the
start and stop time and ignore measurement interval if they are the same.

I realize that the code you use is borrowed straight from the GetIdleTime
function documentation, but the sample code on MSDN has its share of
mistakes.

--
Alex Feinman
---
Visit http://www.opennetcf.org
Rob S said:
What's wrong with this code?

[DllImport ("coredll.dll", SetLastError=false)]
public static extern int GetIdleTime();

[DllImport ("coredll.dll", SetLastError=false)]
public static extern int GetTickCount();


try
{
int dwStartTick = GetTickCount();
int dwIdleSt = GetIdleTime();

Application.DoEvents();

int dwStopTick = GetTickCount();
int dwIdleEd = GetIdleTime();
int PercentIdle = ((100*(dwIdleEd - dwIdleSt)) / (dwStopTick -
dwStartTick));

MessageBox.Show(PercentIdle.ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

the main body of the code in a timer which fires every 20 seconds.

The messagebox shows "0" the first time it runs then i get a divde by zero
error after that.

any ideas?

I need to give focus back to my application if the pda is idle for longer
than 30 seconds.

Thanks guys.
 
I tried using GetIdleTime and it provides very inconsistent results. The
ratio is about 46% on the emulator (when idle), dropping down to 10-20% when
in use. On a PPC Phone Edition it is constantly 0 - the function is
apparently useless on it.

Consider opening and waiting for the following event:
"PowerManager/UserActivity_Inactive" (Using CreateEvent and
WaitForMultipleObjects, or using EventEx class available in OpenNETCF SDF)

You cannot control the timeout period but the upside is that this is when
the OS believes that the user is idle.

--
Alex Feinman
---
Visit http://www.opennetcf.org
Rob S said:
Right ok....

Is there anyway one of you two can give me a small example of this
working,
i'm kind of new to this PDA stuff and i'm running a bit short of time..

All i need is to give focus back to my application if the pda is idle for
more than 30 seconds.

Thanks, really appreciate it.

Alex Feinman said:
For starters, GetTickCount does not give you a very precise interval
measurement. You would be better off using QueryPerformanceCounters.
Because
of its coarsness, it is likely to return the same value before and after
DoEvents resulting in divide by zero condition. You should alway compare
the
start and stop time and ignore measurement interval if they are the same.

I realize that the code you use is borrowed straight from the GetIdleTime
function documentation, but the sample code on MSDN has its share of
mistakes.

--
Alex Feinman
---
Visit http://www.opennetcf.org
Rob S said:
What's wrong with this code?

[DllImport ("coredll.dll", SetLastError=false)]
public static extern int GetIdleTime();

[DllImport ("coredll.dll", SetLastError=false)]
public static extern int GetTickCount();


try
{
int dwStartTick = GetTickCount();
int dwIdleSt = GetIdleTime();

Application.DoEvents();

int dwStopTick = GetTickCount();
int dwIdleEd = GetIdleTime();
int PercentIdle = ((100*(dwIdleEd - dwIdleSt)) / (dwStopTick -
dwStartTick));

MessageBox.Show(PercentIdle.ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

the main body of the code in a timer which fires every 20 seconds.

The messagebox shows "0" the first time it runs then i get a divde by
zero
error after that.

any ideas?

I need to give focus back to my application if the pda is idle for
longer
than 30 seconds.

Thanks guys.
 
Implementation of GetIdleTime isn't required (and isn't always easy to get
on ARM processors), though if it's fot it should be returning MAXDWORD
instead of 0.

--
Chris Tacke
Co-founder
OpenNETCF.org
Has OpenNETCF helped you? Consider donating to support us!
http://www.opennetcf.org/donate


Alex Feinman said:
I tried using GetIdleTime and it provides very inconsistent results. The
ratio is about 46% on the emulator (when idle), dropping down to 10-20%
when in use. On a PPC Phone Edition it is constantly 0 - the function is
apparently useless on it.

Consider opening and waiting for the following event:
"PowerManager/UserActivity_Inactive" (Using CreateEvent and
WaitForMultipleObjects, or using EventEx class available in OpenNETCF SDF)

You cannot control the timeout period but the upside is that this is when
the OS believes that the user is idle.

--
Alex Feinman
---
Visit http://www.opennetcf.org
Rob S said:
Right ok....

Is there anyway one of you two can give me a small example of this
working,
i'm kind of new to this PDA stuff and i'm running a bit short of time..

All i need is to give focus back to my application if the pda is idle for
more than 30 seconds.

Thanks, really appreciate it.

Alex Feinman said:
For starters, GetTickCount does not give you a very precise interval
measurement. You would be better off using QueryPerformanceCounters.
Because
of its coarsness, it is likely to return the same value before and after
DoEvents resulting in divide by zero condition. You should alway compare
the
start and stop time and ignore measurement interval if they are the
same.

I realize that the code you use is borrowed straight from the
GetIdleTime
function documentation, but the sample code on MSDN has its share of
mistakes.

--
Alex Feinman
---
Visit http://www.opennetcf.org
What's wrong with this code?

[DllImport ("coredll.dll", SetLastError=false)]
public static extern int GetIdleTime();

[DllImport ("coredll.dll", SetLastError=false)]
public static extern int GetTickCount();


try
{
int dwStartTick = GetTickCount();
int dwIdleSt = GetIdleTime();

Application.DoEvents();

int dwStopTick = GetTickCount();
int dwIdleEd = GetIdleTime();
int PercentIdle = ((100*(dwIdleEd - dwIdleSt)) / (dwStopTick -
dwStartTick));

MessageBox.Show(PercentIdle.ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

the main body of the code in a timer which fires every 20 seconds.

The messagebox shows "0" the first time it runs then i get a divde by
zero
error after that.

any ideas?

I need to give focus back to my application if the pda is idle for
longer
than 30 seconds.

Thanks guys.
 
Back
Top