By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,931 Members | 1,514 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,931 IT Pros & Developers. It's quick & easy.

tell if idle?

P: n/a
how can i tell if my system has gone idle? then how can i execute a command
accordingly? thnks
Nov 21 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"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

P: n/a
"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

P: n/a
"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

P: n/a
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

P: n/a

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

P: n/a
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

P: n/a

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 discussion thread is closed

Replies have been disabled for this discussion.