tell if idle?

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

Guest

how can i tell if my system has gone idle? then how can i execute a command
accordingly? thnks
 
iwdu15 said:
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
 
iwdu15 said:
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-us/dllproc/base/scheduling_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
 
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
 
Pay $50 for Diskkeeper Workstation. It does this and you save your time for
other projects.

Mike Ober.
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top