Jump to content

How to close a gui quicker than it does!


Go to solution Solved by Luke94,

Recommended Posts

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

#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

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 by Deye
Link to comment
Share on other sites

  • Solution

Have you tried using OnEvent functions?

#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

try that :

#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

:)

Link to comment
Share on other sites

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 :D

Link to comment
Share on other sites

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 :D

As mine was marked as the solution, I've passed the thanks onto Resiak1811. :D

Link to comment
Share on other sites

don't worry .. I'm not mad at you :P

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

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