SnarfSnarf Posted February 18, 2013 Share Posted February 18, 2013 (edited) I'm relatively new to AutoIT, and have put together a script/gui to map/unmap a network drive. It works well, but there are several things I want to add and I'm not sure how. Would really appreciate someone educating me on the proper way to do these things.After the drive is mapped, if the application receives no input for an hour, I want to delete the drive map. I do not want this to prevent users from using a GUI button to disconnect early.If drive is being unmapped after the aforementioned inactivity period, I want to display a message that lasts for 1 minute indicating this will happen, with the ability to cancel. (I assume this is similar to what I'm using for the exit confirmation) Pie in the sky (seemed complicated when I researched) it would be cool to show a status bar on the disconnect window counting down the 1 hourIn general, would like to know if I'm doing things right in the code. Note that I'm not done with the calendars bit... just added that part thismorning but I don't think I'll have any trouble figuring out how to work it (just have to open a different webpage for each choice in the list)Here's the code (got the base code from another topic, added onto it using Koda, forums, and help files.)expandcollapse popup#AutoIt3Wrapper_icon=your_icon.ico #AutoIt3Wrapper_Run_Obfuscator=y #obfuscator_parameters=/striponly #NoTrayIcon #include <ButtonConstants.au3> #include <ComboConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> ;home screen $home = GUICreate("[removed title]", 385, 176, -728, 125) GUISetFont(12, 400, 0, "MS Sans Serif") $map = GUICtrlCreateButton("Map the P Drive", 12, 11, 361, 75) GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif") $label_calendar = GUICtrlCreateLabel("Available Calendars", 100, 99, 170, 28) GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif") $calendarlist = GUICtrlCreateCombo("Please Choose...", 65, 131, 241, 28, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL)) GUICtrlSetData(-1, "[removed list items]") GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif") ;connect window $connectwindow = GUICreate("Connect To The P Drive", 337, 198, -706, 343, -1, -1, $home) $username_id = GUICtrlCreateInput("", 119, 26, 153, 32) GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif") $password_id = GUICtrlCreateInput("", 119, 67, 153, 32, $ES_PASSWORD) GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif") $label_username = GUICtrlCreateLabel("username", 45, 30, 60, 25) GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif") $label_password = GUICtrlCreateLabel("Password", 15, 74, 90, 25) GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif") $connect = GUICtrlCreateButton("Connect", 18, 114, 305, 57, BitOr($GUI_SS_DEFAULT_BUTTON, $BS_DEFPUSHBUTTON)) GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif") ;disconnect window $disconnectwindow = GUICreate("Successful Map", 337, 199, -706, 344) $disconnect = GUICtrlCreateButton("Disconnect P Drive", 9, 65, 312, 60) GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif") $label_timeout = GUICtrlCreateLabel("The drive will automatically" & @CR & "disconnect after 1 hour.", 9, 5, 312, 57, $BS_MULTILINE) GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif") $open = GUICtrlCreateButton("Open P Drive", 9, 133, 313, 60) GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif") GUISetState(@SW_SHOW, $home) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE $confirm = MsgBox(1, "Confirm Exit", "Are you sure you want to close this application? The next person who uses this computer may need it. Note: Closing this application will un-map the P Drive.") If $confirm = 1 Then DriveMapDel("P:") Sleep(10) Exit EndIf Case $map DriveMapDel("P:") GUISetState(@SW_show, $connectwindow) Case $connect $username = GUICtrlRead($username_id) $password = GUICtrlRead($password_id) If $username = '' Or $password = '' Then MsgBox(16, 'Error', 'Empty username or password') ContinueLoop EndIf If DriveMapGet("P:") <> '' Then ; very fast MsgBox(16, 'Error', 'The drive letter (P:) is already in use. You must be very talented to be seeing this message.') ContinueLoop EndIf DriveMapAdd("P:", "[network location]", 0, "[domain]" & $username, $password) ; slow If @error Then Switch @error Case 1 $err_message = 'Undefined / Other error. Windows API return code: ' & @extended Case 2 $err_message = 'Access to the remote share was denied' Case 3 $err_message = 'The device is already assigned' Case 4 $err_message = 'Invalid device name' Case 5 $err_message = 'Invalid remote share' Case 6 $err_message = 'Invalid password' EndSwitch MsgBox(16, 'Error', $err_message) Else ; Drive Mapped Sucessfully ; Show Mapped Drive ShellExecute("explorer.exe", "P:") ; Clear Username/Password Values and set focus to username text box GUICtrlSetData($username_id, "") $username = '' GUICtrlSetData($password_id, "") $password = '' GUICtrlSetState($username_id, $GUI_FOCUS) ; Hide Connect Window GUISetState(@SW_DISABLE, $connectwindow) GUISetState(@SW_HIDE, $connectwindow) GUISetState(@SW_SHOW, $disconnectwindow) EndIf Case $disconnect DriveMapDel("P:") ; Hide disconnect window and open the connect window for next user GUISetState(@SW_HIDE, $disconnectwindow) GUISetState(@SW_ENABLE, $connectwindow) Case $open ShellExecute("explorer.exe", "P:") EndSwitch WEnd Edited February 25, 2013 by SnarfSnarf Link to comment Share on other sites More sharing options...
BrewManNH Posted February 18, 2013 Share Posted February 18, 2013 Look at TimerInit/TimerDiff and perhaps in combination with AdLibRegister. If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator Link to comment Share on other sites More sharing options...
SnarfSnarf Posted February 19, 2013 Author Share Posted February 19, 2013 Look at TimerInit/TimerDiff and perhaps in combination with AdLibRegister.From what I read, using adlibregister would pause the script and load the CPU. Also, TimerInt/TimerDiff typically leverage Sleep (which pauses the script). This is undesired behavior. What I need is a way for the script to continue to wait for any of the case statements to trigger but if they do not for 1 hour, unmap P:.Maybe what I'm asking isn't possible? Link to comment Share on other sites More sharing options...
BrewManNH Posted February 19, 2013 Share Posted February 19, 2013 You read that totally wrong. They do exactly the opposite, they don't sleep the script at all, which is why you would want to use them to time things rather than using Sleep to pause the whole script for a period of time. If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator Link to comment Share on other sites More sharing options...
SnarfSnarf Posted February 22, 2013 Author Share Posted February 22, 2013 (edited) OK, I came up with the code below. My question is... 1. Am I doing this in the cleanest possible way? 2. Where exactly should I place this in my existing code to get it to behave the way I want? (i.e. once the drive is mapped, continue to take other commands but 1 hour after mapping, dismount) Global $timeofmap = TimerInit() ;start timer after drive is mapped Global $sincemap = TimerDiff($timeofmap) ; return time since drive was mapped Global $maxmappedtime = 3600000 ;set to 1 hour AdLibRegister("AutoDismount", 5000) ; check every 5 seconds for maxmappedtime to be 1 hour Func AutoDismount() If $sincemap >= $maxmappedtime Then $dismountconfirm = MsgBox(1, "Are you still there? The P Drive will unmap automatically in one minute. Click cancel to prevent this.", 60) If $dismountconfirm = 1 or -1 Then DriveMapDel("P:") ; Hide disconnect window and enable the connect window for next user GUISetState(@SW_HIDE, $disconnectwindow) GUISetState(@SW_ENABLE, $connectwindow) Else $timeofmap = TimerInit() EndIf EndIf EndFunc Edited February 22, 2013 by SnarfSnarf Link to comment Share on other sites More sharing options...
PhoenixXL Posted February 22, 2013 Share Posted February 22, 2013 There is no where the Script to hook up Hence it would exit before the Registered function is executed Place a loop at the end of the script so that it continues its lifeWhile 1 Sleep(10) Wend My code: PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners. MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression. Link to comment Share on other sites More sharing options...
PhoenixXL Posted February 22, 2013 Share Posted February 22, 2013 This should do it in a more precise mannerGlobal $maxmappedtime = 3600000 ;set to 1 hour AdlibRegister("AutoDismount", $maxmappedtime) ; Execute every 1 hour Func AutoDismount() $dismountconfirm = MsgBox(1, "Are you still there? The P Drive will unmap automatically in one minute. Click cancel to prevent this.", 60) If $dismountconfirm = 1 Or - 1 Then DriveMapDel("P:") ; Hide disconnect window and enable the connect window for next user GUISetState(@SW_HIDE, $disconnectwindow) GUISetState(@SW_ENABLE, $connectwindow) EndIf EndFunc ;==>AutoDismount While 1 Sleep(10) WEnd Plus: TimerDiff will return the Difference when you call it hence your logic was wrong in the script Regards My code: PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners. MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression. Link to comment Share on other sites More sharing options...
BrewManNH Posted February 22, 2013 Share Posted February 22, 2013 It won't work because this section... Func AutoDismount() If $sincemap >= $maxmappedtime Then never updates the $sincemap value, it's always going to be the first value you set at the top of your snippet. You need to update $sincemap everytime you enter the function, or somewhere else in your script. Or just get rid of the $sincemap variable and check the timer directly. Func AutoDismount() If TimerDiff($timeofmap) >= $maxmappedtime Then If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator Link to comment Share on other sites More sharing options...
SnarfSnarf Posted February 22, 2013 Author Share Posted February 22, 2013 Thanks to both of you. PhoenixXL, I think you misunderstood my intention for the code... I believe I do still need the timer. In my original code, I have a while/wend loop handing the events from the GUI. After the $connect event maps a drive, I want a timer to start and after 1 hour for the drive to be dismounted. The user can cancel this, which would also reset the timer. I think I can clean it up from here... one more dumb question though: where should I place the adlibregister in my original post's code to ensure it runs the function after 1 hour of the drive being mapped? Also, does it matter where I put the function definition? I assume it should be outside the while/wend stuff. Link to comment Share on other sites More sharing options...
PhoenixXL Posted February 23, 2013 Share Posted February 23, 2013 (edited) There isnt a need to use both AdlibRegister and TimerFunctionsGlobal $maxmappedtime = 3600000 ;set to 1 hour ;The point where you want to initiate Global $iTimer = TimerInit() Func AutoDismount() $dismountconfirm = MsgBox(1, "Are you still there? The P Drive will unmap automatically in one minute. Click cancel to prevent this.", 60) If $dismountconfirm = 1 Or - 1 Then DriveMapDel("P:") ; Hide disconnect window and enable the connect window for next user GUISetState(@SW_HIDE, $disconnectwindow) GUISetState(@SW_ENABLE, $connectwindow) EndIf EndFunc ;==>AutoDismount While 1 If TimerDiff($iTimer) >= $maxmappedtime Then AutoDismount() $iTimer = TimerInit();Reset the timer neglecting whether the user pressed ok or cancel in the msgbox EndIf WEnd There might be accuracy related issues regarding AdlibRegister, you can place the timer in the main loop. You can define the function anywhere in the script though not inside a loop Regards Edited February 23, 2013 by PhoenixXL My code: PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners. MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression. Link to comment Share on other sites More sharing options...
SnarfSnarf Posted February 25, 2013 Author Share Posted February 25, 2013 (edited) There isnt a need to use both AdlibRegister and TimerFunctionsGlobal $maxmappedtime = 3600000 ;set to 1 hour ;The point where you want to initiate Global $iTimer = TimerInit() Func AutoDismount() $dismountconfirm = MsgBox(1, "Are you still there? The P Drive will unmap automatically in one minute. Click cancel to prevent this.", 60) If $dismountconfirm = 1 Or - 1 Then DriveMapDel("P:") ; Hide disconnect window and enable the connect window for next user GUISetState(@SW_HIDE, $disconnectwindow) GUISetState(@SW_ENABLE, $connectwindow) EndIf EndFunc ;==>AutoDismount While 1 If TimerDiff($iTimer) >= $maxmappedtime Then AutoDismount() $iTimer = TimerInit();Reset the timer neglecting whether the user pressed ok or cancel in the msgbox EndIf WEnd There might be accuracy related issues regarding AdlibRegister, you can place the timer in the main loop. You can define the function anywhere in the script though not inside a loop Regards How does this fit in with my case statements in the original code? I want this the timer to start when the $connect case completes sucessfully and only be checking to see if it has been going more than an hour while the $disconnectwindow is active/shown. I think this is the reason that BrewManNH reccomended using adlibregister ... so I could start the timer when the $connect case happens and be periodically checking for the timer being over an hour at all times. Does this sound right? Here's my updated code... I put the hour variable and function right below the gui definitions, the adlibregister above the case statements in the while loop, and declared the timer variable (thereby starting it?) at the end of the $connect case. Edited February 25, 2013 by SnarfSnarf Link to comment Share on other sites More sharing options...
PhoenixXL Posted February 25, 2013 Share Posted February 25, 2013 (edited) adlibregister registers a function to be executed continuously in a specific amount of time till adlibunregister is called or the script is exittedhence there is no requirement to call it in a loop, registering it once is sufficient.I recently read in a topic that the function was executing in incorrect intervalsHence if accuracy is a priority TimerInit and TimerDiff should be used in a loopTimerInit(could be anywhere) is used to initiate a timer and the TimerDiff should be checked inside a loop( for accuracy).When the elapsed time is greater or equal than required time the function should be executed.Regards Edited February 25, 2013 by PhoenixXL My code: PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners. MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted February 25, 2013 Moderators Share Posted February 25, 2013 PhoenixXL,I recently read in a topic that the function was executing in incorrect intervalsThat thread was a complete red herring - the OP admitted later that it was a coding error on his part that casued the problem. Adlib is as accurate as the PC clock - which is not that wonderful when dealing with very precise timings. m23  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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area  Link to comment Share on other sites More sharing options...
PhoenixXL Posted February 25, 2013 Share Posted February 25, 2013 the OP admitted later that it was a coding error on his part that casued the problem.Sorry, didn't notice the further changes My code: PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners. MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression. Link to comment Share on other sites More sharing options...
SnarfSnarf Posted February 25, 2013 Author Share Posted February 25, 2013 (edited) I think I've got it. Tested this with lower time values to make sure it was working properly. Posting code so that...Anyone who is interested can see what I didMods/Experts can let me know if I did anything wrong/poorly (so I can correct)Here's how the timer part works:1) Map Drive - timer starts and so does function to check for timer to reach 1 hour2) When timer reaches 1 hour, drive is dismounted and function is stopped (notification message appears for 1 minute)Users can cancel the dismount when message appears (resets timer), and dismounting the drive from the gui stops the function (and essentially resets the timer, since connecting will reset it).expandcollapse popup#AutoIt3Wrapper_icon=your_icon.ico #AutoIt3Wrapper_Run_Obfuscator=y #obfuscator_parameters=/striponly #NoTrayIcon #include <ButtonConstants.au3> #include <ComboConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> ;home screen $home = GUICreate("[removed title]", 385, 176, -728, 125) GUISetFont(12, 400, 0, "MS Sans Serif") $map = GUICtrlCreateButton("Map the P Drive", 12, 11, 361, 75) GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif") $label_calendar = GUICtrlCreateLabel("Available Calendars", 100, 99, 170, 28) GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif") $calendarlist = GUICtrlCreateCombo("Please Choose...", 65, 131, 241, 28, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL)) GUICtrlSetData(-1, "[removed list]") GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif") ;connect window $connectwindow = GUICreate("Connect To The P Drive", 337, 198, -706, 343, -1, -1, $home) $username_id = GUICtrlCreateInput("", 119, 26, 153, 32) GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif") $password_id = GUICtrlCreateInput("", 119, 67, 153, 32, $ES_PASSWORD) GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif") $label_netID = GUICtrlCreateLabel("netID", 45, 30, 60, 25) GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif") $label_password = GUICtrlCreateLabel("Password", 15, 74, 90, 25) GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif") $connect = GUICtrlCreateButton("Connect", 18, 114, 305, 57, BitOr($GUI_SS_DEFAULT_BUTTON, $BS_DEFPUSHBUTTON)) GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif") ;disconnect window $disconnectwindow = GUICreate("Successful Map", 336, 148, 323, 232) $disconnect = GUICtrlCreateButton("Disconnect P Drive", 9, 9, 312, 60) GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif") $open = GUICtrlCreateButton("Open P Drive", 9, 77, 313, 60) GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif") GUISetState(@SW_SHOW, $home) Global $maxmappedtime = 3600000 ;set to 1 hour Global $timeofmap = TimerInit() ;this value is reset later on the $connect case Func AutoDismount() ; checks for the drive to be mapped an hour and if true disconnect If TimerDiff($timeofmap) >= $maxmappedtime Then $dismountconfirm = MsgBox(1, "Auto-Dismount Notification", "Are you still there? The P Drive will unmap automatically in one minute. Click cancel to prevent this.", 60) If $dismountconfirm = 2 Then $timeofmap = TimerInit() Else DriveMapDel("P:") ; Hide disconnect window and enable the connect window for next user GUISetState(@SW_HIDE, $disconnectwindow) GUISetState(@SW_ENABLE, $connectwindow) $timeofmap = TimerInit() AdLibUnRegister("AutoDismount") ; stop checking for time to be 1 hour EndIf EndIf EndFunc While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE $confirm = MsgBox(1, "Confirm Exit", "Are you sure you want to close this application? The next person who uses this computer may need it. Note: Closing this application will un-map the P Drive.") If $confirm = 1 Then DriveMapDel("P:") Exit EndIf Case $map DriveMapDel("P:") GUISetState(@SW_show, $connectwindow) Case $connect $username = GUICtrlRead($username_id) $password = GUICtrlRead($password_id) If $username = '' Or $password = '' Then MsgBox(16, 'Error', 'Empty username or password') ContinueLoop EndIf If DriveMapGet("P:") <> '' Then ; very fast MsgBox(16, 'Error', 'The drive letter (P:) is already in use. You must be very talented to be seeing this message.') ContinueLoop EndIf DriveMapAdd("P:", "[network location]", 0, "[domain]" & $username, $password) ; slow If @error Then Switch @error Case 1 $err_message = 'Undefined / Other error. Windows API return code: ' & @extended Case 2 $err_message = 'Access to the remote share was denied' Case 3 $err_message = 'The device is already assigned' Case 4 $err_message = 'Invalid device name' Case 5 $err_message = 'Invalid remote share' Case 6 $err_message = 'Invalid password' EndSwitch MsgBox(16, 'Error', $err_message) Else ; Drive Mapped Sucessfully ; Show Mapped Drive ShellExecute("explorer.exe", "P:") ; Clear Username/Password Values and set focus to username text box GUICtrlSetData($username_id, "") $username = "" GUICtrlSetData($password_id, "") $password = "" GUICtrlSetState($username_id, $GUI_FOCUS) ; Hide Connect Window GUISetState(@SW_DISABLE, $connectwindow) GUISetState(@SW_HIDE, $connectwindow) GUISetState(@SW_SHOW, $disconnectwindow) $timeofmap = TimerInit() ;start timer after drive is mapped AdLibRegister("AutoDismount", 60000) ; start checking for time to be 1 hour EndIf Case $disconnect DriveMapDel("P:") ; Hide disconnect window and open the connect window for next user GUISetState(@SW_HIDE, $disconnectwindow) GUISetState(@SW_ENABLE, $connectwindow) AdLibUnRegister("AutoDismount") ; stop checking for time to be 1 hour Case $open ShellExecute("explorer.exe", "P:") EndSwitch Sleep(10) WEnd Edited February 25, 2013 by SnarfSnarf Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted February 25, 2013 Moderators Share Posted February 25, 2013 SnarfSnarf,You have not set a specific time for the Adlib function to repeat - at present you are checking every 250ms (the default value). As you are waiting for an hour to elapse, I would have thought a significantly longer period would be sufficient and save CPU cycles - perhaps every minute or so? M23  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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area  Link to comment Share on other sites More sharing options...
SnarfSnarf Posted February 25, 2013 Author Share Posted February 25, 2013 SnarfSnarf,You have not set a specific time for the Adlib function to repeat - at present you are checking every 250ms (the default value). As you are waiting for an hour to elapse, I would have thought a significantly longer period would be sufficient and save CPU cycles - perhaps every minute or so? M23Ah yes, thank you I forgot to change that back. When I was testing it I wanted the default value. Setting it to 60000. It doesn't seem to take any CPU @ the default value though. (updated code) Link to comment Share on other sites More sharing options...
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