Jump to content
Sign in to follow this  
fgthhhh

close program while running

Recommended Posts

fgthhhh

I want to close my program any time by click on X button, but it only work after my program finished working.

the only way to quit is right click on its icon and choose quit.

how can i do it?

thx

Share this post


Link to post
Share on other sites
fgthhhh

no, $GUI_EVENT_CLOSE is only working after my function finished. i need something stronger can exit while even running such as right click on its icon and choose quit

Share this post


Link to post
Share on other sites
smashly

Hi,

Post some code so ppl can advise you on how to accomplish what your after..

It would make life easier for those that would like to help you.

Cheers

Share this post


Link to post
Share on other sites
fgthhhh

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

Share this post


Link to post
Share on other sites
smashly

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

Share this post


Link to post
Share on other sites
fgthhhh

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

Share this post


Link to post
Share on other sites
FlyinRiz

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

Share this post


Link to post
Share on other sites
fgthhhh

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

Share this post


Link to post
Share on other sites
fgthhhh

i put this line "If $eMSG = $GUI_EVENT_CLOSE Then Exit"

but somehow it doesn't work? can you help me why?

Share this post


Link to post
Share on other sites
JackDinn

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

Share this post


Link to post
Share on other sites
fgthhhh

really thanks, mate. it worked perfectly!!!

;):D:D:D:D

Share this post


Link to post
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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.