Jump to content
Sign in to follow this  
trung0407

How to run in same time

Recommended Posts

trung0407

For example

$Button1 = GUICtrlCreateButton("Start", 140, 20, 75, 30, 0)
GUICtrlSetOnEvent(-1, "_start")

Func _start()
     While 1
           Sleep(100)
     Wend
EndFunc

So if i click that button, i'll be in an infinite loop.

I want to do other things at the same time like clicking other button to do other function...

how can i make it?

Share this post


Link to post
Share on other sites
someone

What you're talking about doing is multi-threading, and the answer is no. Theres been a lot of discussion about it, I recommend you do a search on the forum for a more inclusive answer then what I can give you.

Bottom line is you can't actually do two things at once, but more often then not you can find ways to code what you need.

Edited by someone

While ProcessExists('Andrews bad day.exe')
	BlockInput(1)
	SoundPlay('Music.wav')
	SoundSetWaveVolume('Louder')
WEnd

Share this post


Link to post
Share on other sites
martin

For example

$Button1 = GUICtrlCreateButton("Start", 140, 20, 75, 30, 0)
GUICtrlSetOnEvent(-1, "_start")

Func _start()
     While 1
           Sleep(100)
     Wend
EndFunc

So if i click that button, i'll be in an infinite loop.

I want to do other things at the same time like clicking other button to do other function...

how can i make it?

My understanding is that in onevent mode it works like this. If you set a function for an event, say but1 pressed call func start(), then when the button but1 is pressed the function is called and nothing will happen with other events until that function returns.

In the example you gave there is a way round this I think.

But first see my example 1 below to demonstrate that a second event will not interrupt the first one.

#include <guiconstants.au3>
HotKeySet("^z", "exitleft");escape route
Opt("GUIOnEventMode", 1)
$gui = GUICreate("2 at once")
$Button1 = GUICtrlCreateButton("Start", 140, 20, 75, 30, 0)
GUICtrlSetOnEvent(-1, "_start")
$Button2 = GUICtrlCreateButton("stop", 140, 60, 75, 30, 0)
GUICtrlSetOnEvent(-1, "_stop")
GUISetState()
GUISetOnEvent($GUI_EVENT_CLOSE, "exitleft")
$st = False
While 1
    
    Sleep(90)
    
WEnd

Func _stop()
    $st = False
    ConsoleWrite("stopped" & @CRLF)
EndFunc  ;==>_stop


Func _start()
    $st = True
    
    While $st
        ConsoleWrite("sstarting" & @CRLF)
        Sleep(100)
    WEnd
    
EndFunc  ;==>_start


Func exitleft ()
    
    Exit
EndFunc  ;==>exitleft

Click the stop button and every time it will print out a line in the console output.

Click the "start" button. Then you will find that clicking the stop button has no effect because the function called by the start button event has not ended. ( BTW HotKeySet can interrupt the function.)

Example 2 below shows a way round this. In this script clicking the stop button will stop the function which was indirectly called by the start button.

#include <guiconstants.au3>
HotKeySet("^z", "exitleft");escape route
Opt("GUIOnEventMode", 1)
$gui = GUICreate("2 at once")
$Button1 = GUICtrlCreateButton("Start", 140, 20, 75, 30, 0)
GUICtrlSetOnEvent(-1, "_start")
$Button2 = GUICtrlCreateButton("stop", 140, 60, 75, 30, 0)
GUICtrlSetOnEvent(-1, "_stop")
GUISetState()
GUISetOnEvent($GUI_EVENT_CLOSE, "exitleft")
$st = False
While 1
    
    Sleep(90)
    
    If $st Then _st2 ();we call the function indirectly

WEnd

Func _stop()
    $st = False
    ConsoleWrite("stopped" & @CRLF)
EndFunc  ;==>_stop


Func _start()
    $st = True
    
EndFunc  ;==>_start

Func _st2 ()
    While $st
        ConsoleWrite("sstarting" & @CRLF)
        Sleep(100)
    WEnd

EndFunc  ;==>_st2

Func exitleft ()
    
    Exit
EndFunc  ;==>exitleft

So using the example 2 method you can have an event which calls an unending function, but a second event can execute it's its own function interrupting the unending function after which the unending function continues.

Not sure I explained that very well.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites
trung0407

So if I understand correctly. A GUICtrlSetOnEvent function can interrupt a loop "while" in the main loop, but cannot interrupt a loop in a function called by another GUICtrlSetOnEvent, right?

Share this post


Link to post
Share on other sites
martin

So if I understand correctly. A GUICtrlSetOnEvent function can interrupt a loop "while" in the main loop, but cannot interrupt a loop in a function called by another GUICtrlSetOnEvent, right?

So it seems to me.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

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  

×