473,386 Members | 2,129 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,386 software developers and data experts.

tell if idle?

how can i tell if my system has gone idle? then how can i execute a command
accordingly? thnks
Nov 21 '05 #1
7 1965
"iwdu15" <iw****@discussions.microsoft.com> wrote in message
news:94**********************************@microsof t.com...
how can i tell if my system has gone idle? then how can i execute a
command
accordingly? thnks


Hi,

You can use class i wrote for my project:

--
Imports System.Timers
Imports System.Runtime.InteropServices

Public Class IdleWatcher
Implements IDisposable

<StructLayout(LayoutKind.Sequential)> _
Public Structure LASTINPUTINFO
Public cbSize As Integer
Public dwTime As Integer
End Structure

<DllImport("user32.dll", SetLastError:=True)> _
Public Shared Function GetLastInputInfo(ByRef lii As LASTINPUTINFO) As
Boolean
End Function

Private Const DEFAULT_CHECK_INTERVAL As Integer = 500

Private _checkinterval As Integer
Private _maxidletime As Integer
Private WithEvents _timer As Timer = Nothing

Public Event Idle(ByVal sender As Object, ByVal e As EventArgs)

Sub New()
Me.New(DEFAULT_CHECK_INTERVAL)
End Sub

Sub New(ByVal checkinterval As Integer)
_checkinterval = checkinterval
End Sub

Public Sub Dispose() Implements System.IDisposable.Dispose
[Stop]()
End Sub

Public Sub Start(ByVal maxidletime As Integer)
_maxidletime = maxidletime * 1000
_timer = New Timer(_checkinterval)
_timer.Start()
End Sub

Public Sub [Stop]()
If Not _timer Is Nothing Then
_timer.Stop()
_timer.Dispose()
_timer = Nothing
End If
End Sub

Private Sub _timer_Elapsed(ByVal sender As Object, ByVal e As
System.Timers.ElapsedEventArgs) Handles _timer.Elapsed
Dim lii As New LASTINPUTINFO
lii.cbSize = Marshal.SizeOf(lii)

If GetLastInputInfo(lii) Then
Dim intIdle As Integer = System.Environment.TickCount -
lii.dwTime
If intIdle >= _maxidletime Then
_timer.Stop()
_timer = Nothing
RaiseEvent Idle(Me, New EventArgs)
End If
Else
Debug.WriteLine("IdleWatcher Win32 ERROR: " +
Marshal.GetLastWin32Error().ToString())
End If

lii = Nothing
End Sub

End Class
--

i hope this is what you are looking for

Regards,
Josip Habjan
URL: http://www.habjansoftware.com

Nov 21 '05 #2
"iwdu15" <iw****@discussions.microsoft.com> schrieb
how can i tell if my system has gone idle? then how can i execute a
command accordingly? thnks

Handle System.Windows.Forms.Application.Idle. It's shared. This is related
to you own app. Or I'd better say, it's related to the current thread. It is
raised when all messages have been processed.

For the whole system it's a little bit more complicated. Idle is not really
idle because as soon as you're running whenever it's idle, it's not idle
anymore... It's better to say to execute something with the lowest priority
possible. Imagine there are two threads running, and both are set to "run
only if idle". What should they do? Which one should wait for the other to
finish?

Actually you have to set the _base priority_ of a thread. This is a
combination of the priority class of the process and the priority level of
the thread. Using the Framwork classes, you can set a thread's priority
level to System.Threading.ThreadPriority.Lowest. To set the process'
priority class, call System.Diagnostics.Process.GetCurrentProcess and set
it's PriorityClass property to ProcessPriorityClass.Idle. Please note that
this affects the whole process.

The final base priority is described here (table at the bottom):
http://msdn.microsoft.com/library/en...priorities.asp
As you see, there's also a THREAD_PRIORITY_IDLE priority for threads. This
is no available in the Thread class. However, there's a
System.Diagnostics.ProcessThread.PriorityLevel property. Unfortunatelly, I
haven't figured out how to find the corresponding ProcessThread object to a
Thread object to be able to set it to Idle. This shouldn't matter too much
because setting the thread's priority to Lowest is usually sufficient.
Setting it to Idle merely reduces the probability of being executed. This
would result in a let-me-be-the-lowest-priority-thread race which wouldn't
lead to anything in the end.
Armin
Nov 21 '05 #3
"iwdu15" <iw****@discussions.microsoft.com> schrieb:
how can i tell if my system has gone idle? then how can i execute a
command
accordingly?


<URL:http://groups.google.de/groups?selm=epRV2mErEHA.324%40TK2MSFTNGP11.phx.gbl >

HOWTO track a user's idle time
<URL:http://www.codeproject.com/dll/trackuseridle.asp>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #4
hi, sry if i wasnt specific enough, i have a program that runs defrag and
starts the defragging the system, but i dont kno how to tell when its done so
i was looking into when the system was idle. i want to shutdown the computer
once the defrag is done, so if anyone can help me on this thatd b great
Nov 21 '05 #5

Pay $50 for Diskkeeper Workstation. It does this and you save your time for
other projects.

Mike Ober.

"iwdu15" <iw****@discussions.microsoft.com> wrote in message
news:7F**********************************@microsof t.com...
hi, sry if i wasnt specific enough, i have a program that runs defrag and
starts the defragging the system, but i dont kno how to tell when its done so i was looking into when the system was idle. i want to shutdown the computer once the defrag is done, so if anyone can help me on this thatd b great


Nov 21 '05 #6
i was wondering if its possible, cause im a relativly new programmer, so i
dont have to save time for any projects, imn just programming this in my free
time to learn everything i can about the language, so if there is a way, id
like to kno but if there isnt il take a look into that software you told me
about
Nov 21 '05 #7

In that case, take a look at the MSDN section on the Task Scheduler
http://msdn.microsoft.com/library/de...start_page.asp.
You have to instruct the system to start your code when it's idle. There is
no system wide message alerting applications to the "System Idle" state.

Mike Ober.
"iwdu15" <iw****@discussions.microsoft.com> wrote in message
news:D1**********************************@microsof t.com...
i was wondering if its possible, cause im a relativly new programmer, so i
dont have to save time for any projects, imn just programming this in my free time to learn everything i can about the language, so if there is a way, id like to kno but if there isnt il take a look into that software you told me about


Nov 21 '05 #8

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

Similar topics

16
by: Kerry Neilson | last post by:
For the past couple of months, Idle won't start when I invoke it. I am at a complete loss for why this is. When this happens, they python command line still starts, and python works fine...
1
by: Moosebumps | last post by:
So say I am working on two separate .py files in IDLE that are part of the same program. Does anyone have problems where when you modify one file, and then run the other, the changes you made in...
1
by: dbrown2 | last post by:
I typically use IDLE for editing and debug. On Windows at least, IDLE and the standard turtle graphics module do not mix. I think both use Tkinter. For now I use IPython and Jedit when using...
8
by: Jonathan Polley | last post by:
I have one account on a WindowsXP machine that refuses to run IDLE (or any other python script that uses Tk). Other people can login to that PC and IDLE runs just fine, so it is not an...
2
by: Robert D. Young | last post by:
I've probably asked this before, but how can I tell within the program I'm running if the .py files was involved by "run module" in IDLE or by using the .py assoication with the python.exe program?...
19
by: Frank Rizzo | last post by:
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...
5
by: Kevin Buchan | last post by:
How can I tell if the system is 'idle', or the screen saver is running, or the machine is locked? I am writing an application that could really benefit from knowing this information. During...
5
by: Russ P. | last post by:
I've been programming in python for a few years using XEmacs on Solaris and Linux. I've been thinking about trying IDLE for a long time, but either it wasn't available on my system or I...
5
by: dg.google.groups | last post by:
Hi all, Is there any standard way to tell if the user is running from a module or from an interactive shell like IDLE or IPython? The best I've come up with so far is for a function to look at...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.