Jump to content

close program while running


Recommended Posts

it look like this

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3> 

GUICreate("My GUI") ; will create a dialog box that when displayed is centered
$button = GUICtrlCreateButton( "run" , 50, 50, 100)
GUISetState(@SW_SHOW) ; will display an empty dialog box


    While 1
        $msg = GUIGetMsg()

        If $msg = $GUI_EVENT_CLOSE Then Exit
    If $msg = $button Then Sleep(10000)
    WEnd

when i click on button "run" it will sleep for 10s even i click on X button

i want to exit instantly when i click X button

Edited by fgthhhh
Link to comment
Share on other sites

Hi,

Instead of trying to do one big sleep(), try doing multiple small sleeps and check for $GUI_EVENT_CLOSE.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

$hGui = GUICreate("My GUI")
$button = GUICtrlCreateButton( "run" , 50, 50, 100)
GUISetState(@SW_SHOW)


While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $button
            GUICtrlSetState($button, $GUI_DISABLE)
            For $i = 1 To 100
                If Not Mod($i, 10) Then WinSetTitle($hGui, "", "My GUI asleep for: " & $i / 10 & " seconds")
                Sleep(100)
                If GUIGetMsg() = $GUI_EVENT_CLOSE Then Exit
            Next
            WinSetTitle($hGui, "", "My GUI")
            GUICtrlSetState($button, $GUI_ENABLE)
    EndSwitch
WEnd

Cheers

Link to comment
Share on other sites

thanks for the solution.it's a nice way

if i have a long function should i put "check for $GUI_EVENT_CLOSE" in every line? or can i have other way because its size will bigger.

as i said. right click on icon and choose exit is perfect way, can i put it in X button so i won't need insert the $GUI_EVENT_CLOSE line

Link to comment
Share on other sites

thanks for the solution.it's a nice way

if i have a long function should i put "check for $GUI_EVENT_CLOSE" in every line? or can i have other way because its size will bigger.

as i said. right click on icon and choose exit is perfect way, can i put it in X button so i won't need insert the $GUI_EVENT_CLOSE line

Even if you add another button, you'll run into the same problem. You need to repeat the While 1 loop as quick as possible because that's the only way the program can grab the next message. You could also look into OnEvent instead. That would allow a button to interrupt your script.

-Aaron

Link to comment
Share on other sites

i'm going to do an auto-shutdown program but i can't close my program while pressed start. pls help me, this is my code

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

$hGui = GUICreate("My GUI")
    Global $sec = @SEC
$clock = GUICtrlCreateLabel( @HOUR &":"& @MIN &":"& $sec, 260, 40, 120, 40) 
    GUICtrlSetFont(-1, 20)  
 $start = GUICtrlCreateButton( "Start", 150,260,100)
    GUICtrlCreateLabel( "Hour(s)", 250,133,50)
$hour = GuiCtrlCreateInput("0", 200, 130, 40, 20)
    GuiCtrlCreateUpDown(-1) 
    GUICtrlCreateLabel( "Minute(s)", 350,133,50)
 $minute = GuiCtrlCreateInput("10", 300, 130, 40, 20)
    GuiCtrlCreateUpDown(-1)
GUISetState(@SW_SHOW)

GUISetState(@SW_SHOW)
    While 1
        If $sec <> @SEC Then
        GUICtrlSetData( $clock , @HOUR &":"& @MIN &":"& @SEC)
        $sec = @SEC
        EndIf
        Global $eMSG = GUIGetMsg()
        Switch $eMSG
            Case $GUI_EVENT_CLOSE
                _exit()
            Case $start
                start()         
        EndSwitch
    WEnd
    
Func start()
    $total = ( GUICtrlRead($hour)*60 + GUICtrlRead($minute) )*60 
    For $i = 0 to $total
        If $eMSG = $GUI_EVENT_CLOSE Then Exit
        Sleep(1000)
    Next
    Exit
EndFunc
Link to comment
Share on other sites

Its because of your pause ("sleep(1000)") in the loop , it messes up the get message.

Try this :-

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
$hGui = GUICreate("My GUI")
Global $sec = @SEC
$clock = GUICtrlCreateLabel(@HOUR & ":" & @MIN & ":" & $sec, 260, 40, 120, 40)
GUICtrlSetFont(-1, 20)
$start = GUICtrlCreateButton("Start", 150, 260, 100)
GUICtrlCreateLabel("Hour(s)", 250, 133, 50)
$hour = GUICtrlCreateInput("0", 200, 130, 40, 20)
GUICtrlCreateUpdown(-1)
GUICtrlCreateLabel("Minute(s)", 350, 133, 50)
$minute = GUICtrlCreateInput("10", 300, 130, 40, 20)
GUICtrlCreateUpdown(-1)
GUISetState(@SW_SHOW)
GUISetState(@SW_SHOW)
While 1
    If $sec <> @SEC Then
        GUICtrlSetData($clock, @HOUR & ":" & @MIN & ":" & @SEC)
        $sec = @SEC
    EndIf
    Global $eMSG = GUIGetMsg()
    Switch $eMSG
        Case $GUI_EVENT_CLOSE
            Exit
        Case $start
            start()
    EndSwitch
WEnd
Func start()
    GUICtrlSetData($start, "Stop")
    $total = (GUICtrlRead($hour) * 60 + GUICtrlRead($minute)) * 60000 ; get time duration in milliseconds
    $total_count = TimerInit() ; start time count
    While 1
        If $sec <> @SEC Then
            GUICtrlSetData($clock, @HOUR & ":" & @MIN & ":" & @SEC)
            GUICtrlSetColor($clock, 0xff0000)
            $sec = @SEC
        EndIf
        $eMSG = GUIGetMsg()
        If $eMSG = $start Then
            GUICtrlSetData($start, "Start")
            GUICtrlSetColor($clock, 0x000000)
            Return
        EndIf
        If $eMSG = $GUI_EVENT_CLOSE Then Exit
        If TimerDiff($total_count) >= $total Then
            MsgBox(0, '', "Stop")
            Exit
        EndIf
    WEnd
    Exit
EndFunc   ;==>start
Edited by JackDinn

Thx all,Jack Dinn.

 

JD's Auto Internet Speed Tester

JD's Clip Catch (With Screen Shot Helper)

Projects :- AutoIt - My projects

My software never has bugs. It just develops random features. :-D

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