jeremyyy Posted February 5, 2014 Posted February 5, 2014 Hi, I need some help adding some logic to a basic program. Pretty much the system will auto login (registry setting) and the program will run and will logout after X mins being idle but will not logout if no user interaction. Now I would like to add something to notify the user saying hey your session is about to end. if they move the mouse or hit the ok button it will wait for the computer to idle again. #NoTrayIcon #include <Timers.au3> #include <MsgBoxConstants.au3> Const $IdleTimeToLogoff = (1 * 60 * 1000) ; 1 minute, for testing Global $last = 0 Global $triggered = false Global $iTimeout = 10 While(True) Sleep(2 * 1000); 2sec Global $i = _Timer_GetIdleTime() If Not $triggered Then if $i < $last Then $triggered = True EndIf $last = $i ElseIf $i > $IdleTimeToLogoff Then ShellExecute("c:\windows\system32\cmd.exe") Exit EndIf WEnd Verbage wise i was thinking this. MsgBox($MB_SYSTEMMODAL, "Title", "Are you still there? This session will logoff in " & $iTimeout & " seconds, unless you click OK to continue.", $iTimeout) This is where I am at and not sure how to continue. Any feedback would be great. thanks!
Moderators JLogan3o13 Posted February 5, 2014 Moderators Posted February 5, 2014 How about something like this? #include <Constants.au3> #include <MsgBoxConstants.au3> $query = MsgBox($MB_YESNO, "Title", "Hey your session is about to logoff") If $query = $IDNO Then ;your code here Else Shutdown($SD_LOGOFF) EndIf "Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball How to get your question answered on this forum!
jdelaney Posted February 5, 2014 Posted February 5, 2014 (edited) What's the point of the whole 'triggered' block. Simplified: While True If _Timer_GetIdleTime() > $IdleTimeToLogoff Then ; add restart call here ShellExecute("c:\windows\system32\cmd.exe") Exit EndIf Sleep(2000) WEnd Edited February 5, 2014 by jdelaney IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
jeremyyy Posted February 5, 2014 Author Posted February 5, 2014 Thanks for the feedback. the triggered block was my way of working the idle after a user activity vs the system booting up and logging in and being idle cause a login and logout loop. For both examples im not sure how to add in or modify... Noob here with these things.
Moderators JLogan3o13 Posted February 5, 2014 Moderators Posted February 5, 2014 If what I suggested worked for you, put it in after your ElseIf line: ElseIf $i > $IdleTimeToLogoff Then $query = MsgBox($MB_YESNO, "Title", "Hey your session is about to logoff") If $query = $IDNO Then ;Code if they don't want to log off Else Shutdown($SD_LOGOFF) EndIf EndIf Be sure to put #include <Constants.au3> and #include <MsgBoxConstants.au3> at the top of your script. "Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball How to get your question answered on this forum!
kylomas Posted February 5, 2014 Posted February 5, 2014 jeremyyy, An example of monitoring idle time and checking for other active users... expandcollapse popup#include <Constants.au3> #include <WinAPISys.au3> #include <array.au3> #AutoIt3Wrapper_Add_Constants=n HotKeySet("{ESC}", "_exit") Local $count_down = 1 * (15 * 1000) ; 15 seconds in milliseconds Local $secs, $mins, $diff, $hours While 1 $diff = Int($count_down - _winapi_getidletime()) $secs = Int(Mod($diff, 60000) / 1000) $mins = Mod(Int($diff / 60000), 60) $hours = Int(($diff / 60000) / 60) TrayTip('', $hours & ' hours ' & $mins & ' minutes and ' & $secs & ' seconds to shutdown', 0, 0) Sleep(500) If _winapi_getidletime() > $count_down Then ; check for other active users $aRet = get_users() for $1 = 0 to ubound($aRet) - 1 if $aRet[$1][0] <> @UserName and $aRet[$1][0] <> '' then if msgbox($mb_okcancel,'*** Warning ***',$aRet[$1][0] & ' is logged on' & ' from ' & $aRet[$1][1] & @lf &@lf & _ 'Click "Cancel" to cancel shutdown') = 2 then exit endif Next ; no other users and idle time reached MsgBox($mb_ok, 'Armageddon', 'Do your thing here') Exit EndIf WEnd Func _exit() Exit EndFunc ;==>_exit Func get_users() Local $strComputer = "localhost", $aLoggedOn[1][2], $iEle = 0 $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2") $colSessions = $objWMIService.ExecQuery _ ("Select * from Win32_LogonSession Where LogonType = 2 OR LogonType = 10") For $objSession In $colSessions $colList = $objWMIService.ExecQuery("Associators of " _ & "{Win32_LogonSession.LogonId=" & $objSession.LogonId & "} " _ & "Where AssocClass=Win32_LoggedOnUser Role=Dependent") For $objItem In $colList $aLoggedOn[$iEle][0] = $objItem.Name If $objSession.LogonType = 2 Then $aLoggedOn[$iEle][1] = 'Console' Else $aLoggedOn[$iEle][1] = 'RDP / Terminal Services' EndIf $iEle += 1 ReDim $aLoggedOn[UBound($aLoggedOn) + 1][2] Next Next return $aLoggedOn EndFunc ;==>get_users Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill
jeremyyy Posted February 5, 2014 Author Posted February 5, 2014 @FUBAR - Nice process on that one. A bit complex but it does help on something I am working on. @JLogan3o13 - Thanks!!! #NoTrayIcon #include <Timers.au3> #include <MsgBoxConstants.au3> #include <Constants.au3> Const $IdleTimeToLogoff = (10 * 60 * 1000) ; 1 minute, for testing Global $last = 0 Global $triggered = false Local $iTimeout = 20 While(True) Sleep(10 * 1000); 10sec Global $i = _Timer_GetIdleTime() If Not $triggered Then if $i < $last Then $triggered = True EndIf $last = $i ElseIf $i > $IdleTimeToLogoff Then $query = MsgBox($MB_OK, "Are you there?", "Click OK to continue or your session will log off in " & $iTimeout & " seconds...", $iTimeout) If $query = $IDOK Then ;process should stay open till it will exit below Else ShellExecute("c:\windows\system32\shutdown.exe", "-l -f") Exit EndIf EndIf WEnd
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now