graybags Posted July 20, 2022 Share Posted July 20, 2022 Hi, I've got a really simple script that just shows the time of a remote computer. But, due to the way it's written, I can't close the gui immediately. Is there a way around this? Thanks, Graybags expandcollapse popup#include <Constants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <array.au3> $Computer = "xxx" Local $timestamp $GUI = GUICreate( $computer & " time", 150, 80, "", "", BitOR($WS_EX_LAYERED, $WS_EX_MDICHILD)) WinSetOnTop ($GUI, "", 1) GUICtrlSetFont ( -1, "10", "800", "", "Consolas" ) GUICtrlCreateLabel( $timestamp, 20, 10, 120, 350) GUISetState(@SW_SHOW) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop EndSwitch $aTime = TimeCalc($Computer) WEnd Func TimeCalc($Computer) $Cmd_out = _GetDOSOutput("net time \\" & $Computer) If $Cmd_out <> "" Then $Cmd_array = StringSplit($Cmd_out, "is ", 1) $Cmd_array2 = StringSplit($Cmd_array[2], @CRLF, 1) $Cmd_array3 = StringSplit($Cmd_array2[1], " ", 1) $timestamp = $cmd_array3[2] GUICtrlSetFont ( -1, "18", "800", "", "Consolas" ) GUICtrlSetData ( -1, $timestamp ) Sleep(60000) EndIf EndFunc Func _GetDOSOutput($sCommand) Local $iPID, $sOutput = "" $iPID = Run('"' & @ComSpec & '" /c ' & $sCommand, "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD) While 1 $sOutput &= StdoutRead($iPID, False, False) If @error Then ExitLoop EndIf WEnd Return $sOutput EndFunc Link to comment Share on other sites More sharing options...
Deye Posted July 20, 2022 Share Posted July 20, 2022 (edited) Check whether this example solves the issue you are experiencing. #include <Constants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> Global $Computer = @ComputerName, $timestamp $GUI = GUICreate($Computer & " Time", 200, 80, "", "", BitOR($WS_EX_LAYERED, $WS_EX_MDICHILD)) WinSetOnTop($GUI, "", 1) GUICtrlSetFont(-1, "10", "800", "", "Consolas") $htime = GUICtrlCreateLabel($timestamp, 40, 15, 120, 350) TimeCalc() AdlibRegister("TimeCalc", 1000) GUISetState() While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop EndSwitch WEnd Func TimeCalc() Local $Cmd_out = _GetDOSOutput("net time \\" & $Computer), $timestampOld = $timestamp $timestamp = StringRegExp($Cmd_out, "\d\/") ? StringRegExp($Cmd_out, "\d{2}\/.+", 3)[0] : $timestampOld GUICtrlSetData($htime, $timestamp) EndFunc Func _GetDOSOutput($sCommand) Local $iPID, $sOutput = "" $iPID = Run('"' & @ComSpec & '" /c ' & $sCommand, "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD) ProcessWaitClose($iPID) Return StdoutRead($iPID, False, False) EndFunc Edited July 20, 2022 by Deye Link to comment Share on other sites More sharing options...
graybags Posted July 20, 2022 Author Share Posted July 20, 2022 Thanks for the reply. No, that still doesn't close the gui as soon as the "X" is clicked. Link to comment Share on other sites More sharing options...
Deye Posted July 20, 2022 Share Posted July 20, 2022 Try to clean the environment : * close any other running scripts ,Disable AV .etc No problems here Link to comment Share on other sites More sharing options...
Solution Luke94 Posted July 20, 2022 Solution Share Posted July 20, 2022 Have you tried using OnEvent functions? expandcollapse popup#include <Constants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <array.au3> AutoItSetOption('GUIOnEventMode', 1) $Computer = "xxx" Local $timestamp $GUI = GUICreate( $computer & " time", 150, 80, "", "", BitOR($WS_EX_LAYERED, $WS_EX_MDICHILD)) GUISetOnEvent($GUI_EVENT_CLOSE, 'Close') WinSetOnTop ($GUI, "", 1) GUICtrlSetFont ( -1, "10", "800", "", "Consolas" ) GUICtrlCreateLabel( $timestamp, 20, 10, 120, 350) GUISetState(@SW_SHOW) While 1 $aTime = TimeCalc($Computer) WEnd Func TimeCalc($Computer) $Cmd_out = _GetDOSOutput("net time \\" & $Computer) If $Cmd_out <> "" Then $Cmd_array = StringSplit($Cmd_out, "is ", 1) $Cmd_array2 = StringSplit($Cmd_array[2], @CRLF, 1) $Cmd_array3 = StringSplit($Cmd_array2[1], " ", 1) $timestamp = $cmd_array3[2] GUICtrlSetFont ( -1, "18", "800", "", "Consolas" ) GUICtrlSetData ( -1, $timestamp ) Sleep(60000) EndIf EndFunc Func _GetDOSOutput($sCommand) Local $iPID, $sOutput = "" $iPID = Run('"' & @ComSpec & '" /c ' & $sCommand, "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD) While 1 $sOutput &= StdoutRead($iPID, False, False) If @error Then ExitLoop EndIf WEnd Return $sOutput EndFunc Func Close() Exit EndFunc Link to comment Share on other sites More sharing options...
Resiak1811 Posted July 20, 2022 Share Posted July 20, 2022 try that : expandcollapse popup#include <Constants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <array.au3> $Computer = @ComputerName Local $timestamp $GUI = GUICreate($Computer & " time", 150, 80, "", "", BitOR($WS_EX_LAYERED, $WS_EX_MDICHILD)) WinSetOnTop($GUI, "", 1) $Cmd_out = _GetDOSOutput("net time \\" & $Computer) $timestamp = StringRegExp($Cmd_out, "\d{2}:\d{2}:\d{2}", 1) GUICtrlCreateLabel($timestamp[0], 20, 10, 120, 350) GUICtrlSetFont(-1, "18", "800", "", "Consolas") GUISetState(@SW_SHOW) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop EndSwitch $aTime = TimeCalc($Computer) WEnd Func TimeCalc($Computer) $Cmd_out = _GetDOSOutput("net time \\" & $Computer) If $Cmd_out <> "" Then $timestamp = StringRegExp($Cmd_out, "\d{2}:\d{2}:\d{2}", 1) GUICtrlSetData(-1, $timestamp[0]) EndIf EndFunc ;==>TimeCalc Func _GetDOSOutput($sCommand) Local $iPID, $sOutput = "" $iPID = Run('"' & @ComSpec & '" /c ' & $sCommand, "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD) While 1 $sOutput &= StdoutRead($iPID, False, False) If @error Then ExitLoop Sleep(10) WEnd Return $sOutput EndFunc ;==>_GetDOSOutput Luke94 1 Link to comment Share on other sites More sharing options...
graybags Posted July 21, 2022 Author Share Posted July 21, 2022 Excellent, thanks for the replies above. Both of those worked perfectly, but I could only mark one as the solution. Link to comment Share on other sites More sharing options...
pixelsearch Posted July 21, 2022 Share Posted July 21, 2022 1 hour ago, graybags said: Excellent, thanks for the replies above. Both of those worked perfectly, but I could only mark one as the solution. When I face this situation, I never "mark one as the solution" because it could be disappointing for the other person who spent time and provided a good working solution too. Instead, I prefer to "like" both solutions and not mark the thread as solved. I know it's not really good (i.e. not to mark the thread as solved) but as I truly want to thank them both, without any final distinction, then this is the way I found to show them equally my gratefulness. How I wish we could mark more than one person as the solver ! Fortunately, this never happens when there's only 1 good solution Link to comment Share on other sites More sharing options...
Deye Posted July 21, 2022 Share Posted July 21, 2022 Strange, Maybe this could have made the change where OP chose to use ExitLoop Case $GUI_EVENT_CLOSE Exit Link to comment Share on other sites More sharing options...
graybags Posted July 21, 2022 Author Share Posted July 21, 2022 7 hours ago, Deye said: Strange, Maybe this could have made the change where OP chose to use ExitLoop Case $GUI_EVENT_CLOSE Exit Oh dear, that must have been a typo! 🥴 Link to comment Share on other sites More sharing options...
Luke94 Posted July 22, 2022 Share Posted July 22, 2022 21 hours ago, pixelsearch said: When I face this situation, I never "mark one as the solution" because it could be disappointing for the other person who spent time and provided a good working solution too. Instead, I prefer to "like" both solutions and not mark the thread as solved. I know it's not really good (i.e. not to mark the thread as solved) but as I truly want to thank them both, without any final distinction, then this is the way I found to show them equally my gratefulness. How I wish we could mark more than one person as the solver ! Fortunately, this never happens when there's only 1 good solution As mine was marked as the solution, I've passed the thanks onto Resiak1811. pixelsearch 1 Link to comment Share on other sites More sharing options...
Resiak1811 Posted July 22, 2022 Share Posted July 22, 2022 don't worry .. I'm not mad at you but you should use the regex like I do instead of your "4 lines" code $Cmd_array = StringSplit($Cmd_out, "is ", 1) $Cmd_array2 = StringSplit($Cmd_array[2], @CRLF, 1) $Cmd_array3 = StringSplit($Cmd_array2[1], " ", 1) $timestamp = $cmd_array3[2] and you don't have to put this line in your loop : GUICtrlSetFont ( -1, "18", "800", "", "Consolas" ) that's my opinion 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