Jump to content

How to stop function with a button ?


Recommended Posts

Hi

I created a button START,when I click on it it changes to STOP and a function is called,

How can I stop the called function with a click on the button? Thanx

My code:

$start = GUICtrlCreateButton("START",250,200,45)
GUICtrlSetOnEvent($start,"startB")

Func startB()
$status = GUICtrlRead($start)
    If $status = "START" Then
    GUICtrlSetData($start,"STOP")
    wait()
    Else
        GUICtrlSetData($start,"START")
         Stop wait()
    EndIf
EndFunc

Func wait()
Sleep(5000)
EndFunc

I want to stop the wait() with a click on STOP...

Edited by clearguy
Link to comment
Share on other sites

Hi

I created a button START,when I click on it it changes to STOP and a function is called,

How can I stop the called function with a click on the button? Thanx

My code:

$start = GUICtrlCreateButton("START",250,200,45)
GUICtrlSetOnEvent($start,"startB")

Func startB()
$status = GUICtrlRead($start)
    If $status = "START" Then
    GUICtrlSetData($start,"STOP")
    wait()
    Else
        GUICtrlSetData($start,"START")
         Stop wait()
    EndIf
EndFunc

Func wait()
Sleep(5000)
EndFunc

I want to stop the wait() with a click on STOP...

That code doesn't work, it's too incomplete. I don't know what problem is there
Link to comment
Share on other sites

Sorry

here

#include <GUIConstants.au3>

$path = "C:\link.txt"
Opt("GUIOnEventMode", 1); Change to OnEvent mode 
$mainwindow = GUICreate("hi", 400, 300)
GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")
;       LABELS
GUICtrlCreateLabel("Hello world! How are you?", 30, 10)


$Halo = GUICtrlCreateProgress (60,270,240,20)
GUISetState ()
GUICtrlSetColor(-1,32250)

;       BUTTONS TO BOTTOM

$start = GUICtrlCreateButton("START",250,200,45)
;       EVENTS 

GUICtrlSetOnEvent($start,"startB")

GUISetState(@SW_SHOW)
;       INPUTS





While 1
    Sleep(1000); Idle around
  WEnd
Func startB()
$status = GUICtrlRead($start)
    If $status = "START" Then
    GUICtrlSetData($start,"STOP")
    wait()
    Else
        GUICtrlSetData($start,"START")
;what to put here to stop wait() ???
    EndIf
EndFunc
Func CLOSEClicked()
Exit
EndFunc
Func wait()
Sleep(5000)
EndFunc
Edited by clearguy
Link to comment
Share on other sites

Sorry

here

#include <GUIConstants.au3>

$path = "C:\link.txt"
Opt("GUIOnEventMode", 1); Change to OnEvent mode 
$mainwindow = GUICreate("hi", 400, 300)
GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")
;       LABELS
GUICtrlCreateLabel("Hello world! How are you?", 30, 10)
$Halo = GUICtrlCreateProgress (60,270,240,20)
GUISetState ()
GUICtrlSetColor(-1,32250)

;       BUTTONS TO BOTTOM

$start = GUICtrlCreateButton("START",250,200,45)
;       EVENTS 

GUICtrlSetOnEvent($start,"startB")

GUISetState(@SW_SHOW)
;       INPUTS
While 1
    Sleep(1000); Idle around
  WEnd
Func startB()
$status = GUICtrlRead($start)
    If $status = "START" Then
    GUICtrlSetData($start,"STOP")
    wait()
    Else
        GUICtrlSetData($start,"START")
;what to put here to stop wait() ???
    EndIf
EndFunc
Func CLOSEClicked()
Exit
EndFunc
Func wait()
Sleep(5000)
EndFunc

the issue is: "sleep" stops the execution of the script, for that reason it doesn't receive commands

#include <GUIConstants.au3>

$path = "C:\link.txt"
Opt("GUIOnEventMode", 1); Change to OnEvent mode 
$mainwindow = GUICreate("hi", 400, 300)

;       LABELS
GUICtrlCreateLabel("Hello world! How are you?", 30, 10)

$Halo = GUICtrlCreateProgress (60,270,240,20)
GUICtrlSetColor(-1,32250)

;       BUTTONS TO BOTTOM

$start = GUICtrlCreateButton("START",250,200,45)
;       EVENTS 

GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")
GUICtrlSetOnEvent($start,"startB")
GUISetState()
;       INPUTS


While 1
    Sleep(1000); Idle around
WEnd

Func startB()
$status = GUICtrlRead($start)
    If $status = "START" Then
        GUICtrlSetData($start,"STOP")
        wait()
;what to put here to stop wait() ???
    EndIf
EndFunc

Func CLOSEClicked()
    Exit
EndFunc

Func wait()
    $opt=Opt ("GuiOnEventMode",0)
    For $i=1 To 500; five seconds (10 miliseconds of sleep * 500 loops)
        $msg=GUIGetMsg ()
        Sleep(10)
        If $msg=$start Then ExitLoop; it will scan each 10 ms if the button is pressed 
    Next
    Opt ("GuiOnEventMode",$opt) 
    GUICtrlSetData($start,"START");when finish sleep,  button text becomes to "Start"
EndFunc

If I understand the problem, that will work

Edited by elgabionline
Link to comment
Share on other sites

Yes many thanx it works!

I have two questions:what does this code

$opt=Opt ("GuiOnEventMode",0)
??

And if I want to put an application Run("__.exe) in the wait() function,

where have I to put it that it stops when clicked on [stop]=> ProcessClose("__.exe) in wait() ?

Edited by clearguy
Link to comment
Share on other sites

Yes many thanx it works!

I have two questions:what does this code

$opt=Opt ("GuiOnEventMode",0)
??

And if I want to put an application Run("__.exe) in the wait() function,

where have I to put it that it stops=> ProcessClose("__.exe) in wait() ?

$opt save the previous value of Opt ()

Func wait()
    Run ("someprogram.exe"); Executes an external program
    $opt=Opt ("GuiOnEventMode",0)
    For $i=1 To 500; five seconds (10 miliseconds sleep * 500 loops)
        $msg=GUIGetMsg ()
        Sleep(10)
        If $msg=$start Then; it will scan each 10 milisegundos if the button is pressed 
            ProcessClose ("someprogram.exe"); closes the program ONLY if button stop is clicked
            ExitLoop
        EndIf
    Next
    Opt ("GuiOnEventMode",$opt) 
    GUICtrlSetData($start,"START");when finish sleep,  button text becomes to "Start"
EndFunc
Edited by elgabionline
Link to comment
Share on other sites

Hi,

Does HotKeySet() help you out?

So long,

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

#include <GUIConstants.au3>
$i = 0
$Counting = 0
$Form1 = GUICreate("SUPER FREAKING AWSOME COUNTER OF GOD", 479, 180, 192, 125)
$input = GUICtrlCreateInput("", 112, 136, 233, 21, -1, $WS_EX_CLIENTEDGE)
$count = GUICtrlCreateButton("Start Count", 144, 32, 161, 65)
GUISetState(@SW_SHOW)
While 1
    $msg = GUIGetMsg()
    If $msg = $count Then
        $Counting = 1
        GUICtrlSetData($count,"Stop Count")
        While $Counting = 1
            $msg = GUIGetMsg()
            $i += 1; $i = $i + 1
            GUICtrlSetData($input,$i)
            If $msg = $GUI_EVENT_CLOSE Then Exit
            If $msg = $count and $counting = 1 Then
                ExitLoop
            EndIf
        WEnd
        $Counting = 0
        GUICtrlSetData($count,"Start Count")
    EndIf
    If $msg = $GUI_EVENT_CLOSE Then Exit
WEnd
oÝ÷ ØêÚºÚ"µÍÚ[ÛYH    ÑÕRPÛÛÝ[Ë]LÉÝÂÌÍÚHHÛØ[    ÌÍÐÛÝ[[ÂÌÍÑÜLHHÕRPÜX]J  ][ÝÔÕTTPRÒSÈUÔÓÓQHÓÕSTÑÓÑ  ][ÝË

ÎKNNLLJBÌÍÚ[]HÕRPÝÜX]R[]
    ][ÝÉ][ÝËLLLÍÌËKLK    ÌÍÕÔ×ÑVÐÓQSQÑJBÌÍØÛÝ[HÕRPÝÜX]P]Û  ][ÝÔÝÛÝ[   ][ÝËM
ÌMK
JBÕRTÙ]Ý]JÕ×ÔÒÕÊBÚ[HBIÌÍÛÙÈHÕRQÙ]ÙÊ
BRY ÌÍÛÙÈH ÌÍØÛÝ[[BQÕRPÝÙ]]J   ÌÍØÛÝ[ ][ÝÔÝÜÛÝ[ ][ÝÊBBIÌÍÐÛÝ[[ÈHÝ  ÌÍÐÛÝ[[ÂBUÚ[H    ÌÍÐÛÝ[[ÂBBIÌÍÛÙÈHÕRQÙ]ÙÊ
BBBIÌÍÚH
ÏHNÈ  ÌÍÚHH    ÌÍÚH
ÈBBBQÕRPÝÙ]]J   ÌÍÚ[]    ÌÍÚJBBBRY    ÌÍÛÙÈH ÌÍÑÕRWÑUSÐÓÔÑH[^]BBRY  ÌÍÛÙÈH ÌÍØÛÝ[[BBBIÌÍÐÛÝ[[ÈHÝ   ÌÍÐÛÝ[[ÂBBBQ^]ÛÜBBQ[YBUÑ[BQÕRPÝÙ]]J ÌÍØÛÝ[ ][ÝÔÝÛÝ[   ][ÝÊBQ[YRY    ÌÍÛÙÈH ÌÍÑÕRWÑUSÐÓÔÑH[^]Ñ[

2 examples of pausing with the same button

Edited by thatsgreat2345
Link to comment
Share on other sites

Yes it works but this is so annoying to spread this line everywhere in my function :D

Is there not a space in which the GUIGetMsg works without repeating it hundred of times???

My functions became completely incomprehensible :D

Im sure there is another 'softer' way to do it?

If not they have to rapidly create a GUIGetMsg commande,that if it's placed at the begin of a function

it is loaded for all the function....

Please give me some hints this shines impossible :P

Link to comment
Share on other sites

Use...

;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
;Filename: OnEventModeTemp.au3 (Compiled as OnEventModeTemp.exe)
;Used With: no other files
;Created by: Jarvis J Stubblefield (support "at" vortexrevolutions "dot" com)
;Created on: 05/22/2006
;Modified on: 
;Modified by: 
;Used for: OnEventMode Template for GUI Creation
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;---*** Preprocessor ***---
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
#include <GUIConstants.au3>

Opt("GUIOnEventMode", 1) ;Set GUI to ONEvent Mode.

;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;---*** File Installations ***---
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;---*** Registry Information ***---
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;---*** Declare Variables ***---
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Global $wHeight, $wWidth                                                             ;Window Size
Global $m_HWnd, $m_Options, $m_Help, $m_Exit            ;Window Controls

;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;---*** Define Variables ***---
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

$wHeight = 300
$wWidth  = 300

;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;---*** GUI Creation ***---
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

$m_HWnd     = GUICreate("Rename-Index N Find", $wWidth, $wHeight)
$m_Options  = GUICtrlCreateButton("Options", 5, ($wHeight - 25), 94, 20)
$m_Help     = GUICtrlCreateButton("Help", 103, ($wHeight - 25), 94, 20)
$m_Exit     = GUICtrlCreateButton("Exit", 201, ($wHeight - 25), 94, 20)

;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;---*** GUI Set Events ***---
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

GUICtrlSetOnEvent($m_Options, "_CtrlEventHandler")
GUICtrlSetOnEvent($m_Exit, "_CtrlEventHandler")
GUICtrlSetOnEvent($m_Help, "_CtrlEventHandler")

;System Events Handled by One _SysEventHandler()
GUISetOnEvent($GUI_EVENT_CLOSE, "_SysEventHandler", $m_HWnd)
GUISetOnEvent($GUI_EVENT_MINIMIZE, "_SysEventHandler", $m_HWnd)
GUISetOnEvent($GUI_EVENT_RESTORE, "_SysEventHandler", $m_HWnd)
GUISetOnEvent($GUI_EVENT_MAXIMIZE, "_SysEventHandler", $m_HWnd)
GUISetOnEvent($GUI_EVENT_PRIMARYDOWN, "_SysEventHandler", $m_HWnd)
GUISetOnEvent($GUI_EVENT_PRIMARYUP, "_SysEventHandler", $m_HWnd)
GUISetOnEvent($GUI_EVENT_SECONDARYDOWN, "_SysEventHandler", $m_HWnd)
GUISetOnEvent($GUI_EVENT_SECONDARYUP, "_SysEventHandler", $m_HWnd)
GUISetOnEvent($GUI_EVENT_MOUSEMOVE, "_SysEventHandler", $m_HWnd)
GUISetOnEvent($GUI_EVENT_RESIZED, "_SysEventHandler", $m_HWnd)
GUISetOnEvent($GUI_EVENT_DROPPED, "_SysEventHandler", $m_HWnd)

GUISetState(@SW_SHOW, $m_HWnd)

;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;---*** Main Program Loop ***---
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

While 1
    Sleep(10)
WEnd

;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;---*** GUI Event Functions ***---
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Func _CtrlEventHandler()
    Switch @GUI_CtrlId
        Case $m_Exit
            _TerminateApp()
        Case $m_Options
        Case $m_Help
    EndSwitch
EndFunc

Func _SysEventHandler()
    Switch @GUI_CtrlId
        Case $GUI_EVENT_CLOSE
            _TerminateApp()
        Case $GUI_EVENT_MINIMIZE
        Case $GUI_EVENT_RESTORE
        Case $GUI_EVENT_MAXIMIZE
        Case $GUI_EVENT_PRIMARYDOWN
        Case $GUI_EVENT_PRIMARYUP
        Case $GUI_EVENT_SECONDARYDOWN
        Case $GUI_EVENT_SECONDARYUP
        Case $GUI_EVENT_MOUSEMOVE
        Case $GUI_EVENT_RESIZED
        Case $GUI_EVENT_DROPPED
    EndSwitch
EndFunc

;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;---*** Define Functions ***---
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Func _TerminateApp()
    GUIDelete($m_HWnd)
    
    Exit
EndFunc

Use that to have your GUI work while you are then also able to call Functions based on button presses and the works.

:D

JS

AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Link to comment
Share on other sites

Yes it works but this is so annoying to spread this line everywhere in my function :D

Is there not a space in which the GUIGetMsg works without repeating it hundred of times???

My functions became completely incomprehensible :D

Im sure there is another 'softer' way to do it?

If not they have to rapidly create a GUIGetMsg commande,that if it's placed at the begin of a function

it is loaded for all the function....

Please give me some hints this shines impossible :P

Try putting

If $StopNow = 0 Then
...;your function
EndIf

around each of your functions, then use Adlib to set the previously-defined Global variable $StopNow to 1 when the button is pressed...not much better, but at least you're only 'polling' the GUI events in one place.

"There are 10 types of people in this world - those who can read binary, and those who can't.""We've heard that a million monkeys at a million keyboards could produce the complete works of Shakespeare; now, thanks to the Internet, we know that is not true." ~Robert Wilensky0101101 1001010 1100001 1101101 1100101 1110011 0110011 1001101 10001110000101 0000111 0001000 0001110 0001101 0010010 1010110 0100001 1101110
Link to comment
Share on other sites

Try putting

If $StopNow = 0 Then
...;your function
EndIf

around each of your functions, then use Adlib to set the previously-defined Global variable $StopNow to 1 when the button is pressed...not much better, but at least you're only 'polling' the GUI events in one place.

I have used work arounds like this for quite some time until one day I just had the major need for a Event based GUI. I pulled out the help manual and started reading until I came up with the above template. I hope it helps him fix the issue he is having.

JS

AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Link to comment
Share on other sites

Must I include this script?? :">

I've done it but how to use it?

Are you talking about my template?

Its not an include. Look at it. Its a template of a GUI. Run it. It will display a GUI that when you click the buttons does certain events based on those presses. The GUI will continue to function properly even while it is inside function.

Take your code and insert it in the right places, and it should work out for you. You can remove the variables and GUI creation in place of your own. I just always use those variables when creating a GUI.

I hope this helps,

JS

AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

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