Jump to content

Timer no longer working


JonCross
 Share

Recommended Posts

Hello everyone, I run a computer lab at an elementary school and I have been working on a computer lab automation script. Bellow is a portion of the script that runs on the computers. I added the ability to run some commands via the tray icon but now that I have done that the idle timer will not work any ideas?

Here is the code (its kinda long):

#NoTrayIcon
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <Misc.au3>
#include <Timers.au3>
#include <string.au3>
#include <ChangeResolution.au3>
#include <zip.au3>
if _Singleton("Daemon",1) = 0 Then
    Msgbox(0,"Warning","An occurence of Control Daemon is already running.", 10)
    Exit
EndIf
Opt("TrayMenuMode",1)
Global $Server = "\\adm-24-server01\RCData\ComputerAutomation"
Global $Local ="C:\HighlandAutomation"
Global $iIdleTime
$Refresh  = TrayCreateItem("Refresh Computer", -1, 0)
$teacher = TrayCreateItem("Teacher Mode", -1, 1)
$close = TrayCreateItem("Exit", -1, 9)
$admin = TrayCreateItem("Admin Mode", -1, 2)
TraySetState()

While 1
    $iIdleTime = _Timer_GetIdleTime()   
    IF FileExists("C:\thinclient") Then 
        Exit
    ElseIf $iIdleTime > 300000 Then ;#5 Mintue Idle Timer 
        Ping ("adm-24-server01")
        If @error Then
        _Offline()
        endif
    ElseIf $iIdleTime > 1800000 Then ;#30 Minute Idle time
        DesktopRefresh()
    Elseif not ProcessExists("Rules.exe") AND $iIdleTime > 600000 Then ;#10 Minute Idle time
        Run($Local&"\Rules.exe")
    ElseIF FileExists($Server&"\shutdown")Then
        shutdown (8)
    ElseIF FileExists($Server&"\logoff")Then
        Shutdown (0)
    ElseIF FileExists($Server&"\reboot")Then
        Shutdown (2)
    ElseIF FileExists($Server&"\nonet")Then
        ProcessClose("IExplore.exe")
    ElseIF FileExists($Server&"\xattack") Then
        ProcessClose("TimezAttackLauncher.exe")
        ProcessClose("BigBrainz_TimezAttackWin.exe")
    ElseIf FileExists($Server&"\mute") Then
        Send("{VOLUME_MUTE}")
        Sleep(100)
    ElseIf FileExists($Server&"\attentionON")Then
        _Attention()        
    ElseIf FileExists($Server&"\messageON")Then
        _Message()
    ElseIf FileExists($Server&"\TakeAttendance")Then
        _Attendance()
    ElseIf FileExists($Server&"\refresh") Then
        DesktopRefresh()
        Sleep(10000)
    EndIf
    $msg = TrayGetMsg() 
Select
    Case $msg  = 0
        ContinueLoop
    Case $msg = $refresh
        DesktopRefresh()
    Case $msg = $close
        close()
    Case $msg = $teacher
        Teacher()
    Case $msg = $admin
        Controler()
    EndSelect
WEnd
Func Close()
    $var = IniReadSection($Server&"\settings.ini", "exit")
If @error Then 
    MsgBox(4096, "", "Error occurred.")
Else
    For $i = 1 To $var[0][0]
    $password = $var[$i][1]
    $decript = _HexToString($password)
Next    
    $passwd = InputBox("Security Check", "Enter Exit password.", "", "*", 190, 115,-1,-1, 5)
IF $passwd = $decript then  
    Exit
Else
    MsgBox(48, "Wrong Password", "Sorry, that was not the correct Password!",3)
EndIf
EndIf
EndFunc

Func Teacher()
$var = IniReadSection($Server&"\settings.ini", "teacher")
If @error Then 
    MsgBox(4096, "", "Error occurred.")
Else
    For $i = 1 To $var[0][0]
    $password = $var[$i][1]
    $decript = _HexToString($password)
Next    
    $passwd = InputBox("Security Check", "Enter Teacher password.", "", "*", 190, 115,-1,-1,5)
IF $passwd = $decript then  
    TeacherMode()
Else
    MsgBox(48, "Wrong Password", "Sorry, that was not the correct Password!",3)
EndIf
EndIf
EndFunc

Func _Offline()
    TraySetState()
    TrayTip("SERVER OFFLINE!", "THE SERVER IS OFFLINE!", 30, 3)
    While 1
    $msg = Ping ("adm-24-server01")
    Select
        Case $msg = @Error
            TrayTip("SERVER OFFLINE!", "THE SERVER IS OFFLINE!", 30, 3)
            ContinueLoop
        Case $msg > 1
            ExitLoop
        EndSelect
    WEnd
    Autoitsetoption("TrayIconHide", 1)
EndFunc

Func TeacherMode()
    $Exit = TrayCreateItem("Exit Teacher Mode", -1, 1)
    TrayItemDelete($Teacher)
    TraySetState()
    TrayTip("Teacher Mode", "This Computer is in Teacher Mode. Click HERE when Done.", 30, 1)
    While 1
    $msg = TrayGetMsg()
    Select
        Case $msg = 0
            ContinueLoop
        Case $msg = $exit
            ExitLoop
        EndSelect
    WEnd
    TrayItemDelete($Exit)
    $teacher = TrayCreateItem("Teacher Mode", -1, 1)
EndFunc

Func _Attention()
    $var1 = IniReadSection($Server&"\settings.ini", "attention")
If @error Then 
    MsgBox(4096, "", "Error occurred.")
Else
    For $i = 1 To $var1[0][0]
    $color = $var1[$i][1]
Next
BlockInput(1)
GUICreate("Attention", @DesktopWidth + 10, @DesktopHeight +35, -1, -1, -1, $WS_EX_TOPMOST)
GUISetBkColor($color)
GUISetState(@SW_SHOW)
While 1
If FileExists($Server&"\attentionOFF") Then ExitLoop
    WEnd
    GUIDelete()
    BlockInput(0)
    Send("{CTRLDown}", 0)
    sleep(1000)
    Send("{CTRLUP}", 0)
    EndIf
EndFunc

Func _Attendance()
    Local $Name
    $Name = Inputbox("Attendance", "Enter Your First and Last Name","","","","",-1,-1,60)
    WinSetOnTop("Attendance","",1)
    FileWriteLine($Server&"\Attendance.txt", $Name)
    FileClose($Server&"\Attendance.txt")
    MsgBox(0, "Thank You", "Thank You", 10)
EndFunc

Func _Message()
$var2 = IniReadSection($Server&"\settings.ini", "message")
If @error Then 
    MsgBox(4096, "", "Error occurred.")
Else
    For $i = 1 To $var2[0][0]
    $message = $var2[$i][1]
Next
    GUICreate("Attention", 355, 125, -1, -1, $WS_POPUPWINDOW, $WS_EX_TOPMOST)
    GUISetFont(10, 400)
    GUICtrlCreateedit($Message, 1, 1, 325,115, $ES_READONLY, $WS_EX_TRANSPARENT )
    GUISetState()
    While 1
If FileExists($Server&"\messageOFF") Then 
    ExitLoop
    Endif
    WEnd
    GUIDelete()
    EndIf
EndFunc

Func Controler()
    Run($Server&"\Lab Controler.exe")
    Sleep(3000)
    WinWaitclose("Security Check")
    TrayItemDelete($admin)
    TraySetState()
    WinwaitClose ("Computer Lab Controler")
    $Admin = TrayCreateItem("Admin Mode", -1, 2)
EndFunc

Func DesktopRefresh()
BlockInput(1)
;#Change Screen Res
$iWidth = 1024
$iHeight = 768
$iBitsPP = 32
$iRefreshRate = 60
$vRes = _ChangeScreenRes($iWidth, $iHeight, $iBitsPP, $iRefreshRate)
If @error Then
MsgBox(262160, "ERROR", "Unable to change screen - check parameters")
EndIf
;#End Change Screen
SplashImageOn("Welcome to Highland Elementary School", $Local&"\splash.bmp");Splash Screen durring scrip run time
;Deletes all icons on the local accound desktop.
FileDelete(@DesktopDir &"\*.*") 
;Office Icons are created in only Office Version 2002, 2003 and 2007.
IF FileExists("C:\Program Files\Microsoft Office\Office11\WinWord.exe") Then
FileCreateShortcut("C:\Program Files\Microsoft Office\Office11\WinWord.exe", @Desktopdir & "\Word","C:\Program Files\Microsoft Office\Office11","","","C:\Program Files\Microsoft Office\Office11\WinWord.exe")
FileCreateShortcut("C:\Program Files\Microsoft Office\Office11\PowerPNT.exe", @Desktopdir & "\Power Point","C:\Program Files\Microsoft Office\Office11","","","C:\Program Files\Microsoft Office\Office11\PowerPNT.exe")
Elseif FileExists("C:\Program Files\Microsoft Office\Office10\WinWord.exe") Then
FileCreateShortcut("C:\Program Files\Microsoft Office\Office10\WinWord.exe", @Desktopdir & "\Word","C:\Program Files\Microsoft Office\Office10","","","C:\Program Files\Microsoft Office\Office10\WinWord.exe")
FileCreateShortcut("C:\Program Files\Microsoft Office\Office10\PowerPNT.exe", @Desktopdir & "\Power Point","C:\Program Files\Microsoft Office\Office10","","","C:\Program Files\Microsoft Office\Office10\PowerPNT.exe")
Elseif FileExists("C:\Program Files\Microsoft Office\Office12\WinWord.exe") Then
FileCreateShortcut("C:\Program Files\Microsoft Office\Office12\WinWord.exe", @Desktopdir & "\Word","C:\Program Files\Microsoft Office\Office12","","","C:\Program Files\Microsoft Office\Office12\WinWord.exe")
FileCreateShortcut("C:\Program Files\Microsoft Office\Office12\PowerPNT.exe", @Desktopdir & "\Power Point","C:\Program Files\Microsoft Office\Office12","","","C:\Program Files\Microsoft Office\Office12\PowerPNT.exe")
Else 
    EndIF
;Shortcuts will be created recreated
_Zip_UnzipAll($Local&"\Desktop.zip", @DesktopDir, 1)
;Deletes temp Files and Misc Files
FileDelete(@UserProfileDir&"\Cookies\*.txt")
FileDelete(@UserProfileDir&"\Local Settings\Temporary Internet Files\*.*")
FileDelete(@TempDir&"\*.*")
FileDelete(@UserProfileDir&"\Recent\*.*")
FileDelete(@FavoritesDir&"\*.*")
FileDelete(@AppDataDir&"\Microsoft\Internet Explorer\Quick Launch\*.*")
DirRemove(@FavoritesDir&"\Links", 1)
DirRemove(@FavoritesDir&"\Favorites Bar", 1)
ShellExecuteWait("RunDll32.exe"," InetCpl.cpl,ClearMyTracksByProcess 255")
FileRecycleEmpty()
;Computer Lab Display Theme will be applied.
Send("#m",0)
ShellExecute($Local&"\Lab Theme.Theme")
ControlFocus("Display Properties", "", "")
WinwaitActive("Display Properties")
Send("{ENTER}",0)
WinWaitClose("Display Properties")
ControlFocus("Program Manager", "", "")
;Desktop Icons Will be Auto Arragned
Sleep (2000)
MouseClick("Right", 920,25)
Send("I")
Send("A")
BlockInput(0)
Sleep(5000)
Send("{LCTRL}", 0)
SplashOff()
;RESTART Deamon
FileDelete($local&"\Refresher.exe")
FileCopy($server&"\Refresher.exe",$Local,1)
Run($local&"\Refresher.exe",$local)
Exit
EndFunc
Edited by JonCross
Link to comment
Share on other sites

  • Moderators

JonCross,

As you have it set now, there are some major problems with the timer elements in your While...WEnd loop.

To start with, you are firing the timer events permanently once the trigger time has passed. For example, we reach the 5 minute point where you want to ping the server - it fires as we hit 5 minutes, but then also fires every pass through the loop from then on! :lol: Quite a lot of pings!

The next problem is the use of the If...ElseIf...EndIf structure. AutoIt only actions the first true condition on each pass. As you have the 5 minute case as the first option, AutoIt will always fire on that (once you have reached the required time as shown above) and so you will never test the subsequent 10 and 30 minute conditions. :(

To get over the timer problem, I would do something like this:

$iCorr05 = 0
$iCorr10 = 0
$iCorr30 = 0

While 1

    $iIdleTime = Int(_Timer_GetIdleTime() / 1000)

    If $iIdleTime - $iCorr30 > 1800 Then ;#30 Minute Idle time
        $iCorr30 = $iIdleTime
        DesktopRefresh()
    EndIf
    If Not ProcessExists("Rules.exe") And $iIdleTime - $iCorr10 > 600 Then ;#10 Minute Idle time
        $iCorr10 = $iIdleTime
        Run($Local & "\Rules.exe")
    EndIf
    If $iIdleTime - $iCorr05 > 300 Then ;#5 Minute Idle Timer
        $iCorr05 = $iIdleTime
        Ping("adm-24-server01")
        If @error Then
            _Offline()
        EndIf
    EndIf

This way you test all of the delay times and only trigger the events at the required delay by effectively "resetting" the trigger time for each of them - I hope that it is clear from the code how that works. As I have introduced the 3 correction factors which augment each time you pass the respective idle delay, I have also reduced the size of the values they must hold by working in seconds rather than milliseconds so we do not bump up against AutoIt's maximum value limit(although I imagine that your system is never idle that long! :P ).

As I am not familiar with your system, I cannot say whether there is a similar problem lurking in the remainder if your If...ElseIf...EndIf structure, but if my assumption that each event is a unique occurrence and thus you are unlikely to have multiple files appearing simultaneously is correct, the structure seems robust as it is.

Quite why your timimg problems arrived when you added the tray menu is beyond me, but I hope the above helps resolve them. Please come back if it does not - or of you require any more help (not that you look as though you do!). :mellow:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...