Jump to content
Sign in to follow this  
XTensionX

GUI Froze

Recommended Posts

XTensionX

hi everyone,

i am having some problem with my GUI

i have 2 buttons on it and when i press 1 of them the GUI froze and nothing will work, but the script for that button i press continues

could any help me solve this problem please

here is my code:

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


    Global $Form1_1 = GUICreate("EXE", 314, 160)
    Global $Button1 = GUICtrlCreateButton("button1", 176, 32, 113, 33, $WS_GROUP)
    Global $Button2 = GUICtrlCreateButton("button2", 176, 80, 113, 33, $WS_GROUP)
    GUISetState(@SW_SHOW)

    While 1
        
        $nMsg = GUIGetMsg()
        Switch $nMsg
        Case $GUI_EVENT_CLOSE
                Exit

            Case $Button1
while 1
ControlSend("[CLASS:Notepad]", "", "Edit1", "This is a line of text")
sleep(2000)
WEnd
                Case $Button2
                    while 1
ControlSend("[CLASS:Notepad]", "", "Edit1", "TESTTT")
Sleep (32000)
WEnd
        EndSwitch
    WEnd

thanks

Share this post


Link to post
Share on other sites
BrettF

Keep in mind that AutoIt is single threaded. That means realistically only one thing will be happening at any given moment. Armed with that knowledge, what line would stop everything after pressing the button?

Share this post


Link to post
Share on other sites
XTensionX

Keep in mind that AutoIt is single threaded. That means realistically only one thing will be happening at any given moment. Armed with that knowledge, what line would stop everything after pressing the button?

the wend?

Edited by XTensionX

Share this post


Link to post
Share on other sites
BrettF
martin

the wend?

Yes..ish.

When you press a button your script goes into the while/wend loop and stays there going round and round. In the loops the only thing the script is doing is what you tell it to do, ie send a string to NotePad and wait for 2 seconds or 32 seconds. To respond to anything it needs to be looking out for other things. If someone presses a button then the script will only find out by seeing what the result of GuiGetMsg is, so you you could make your loop change from

while 1
 send...
sleep...
Wend

to

while GuiGetMsg <> -3 ;while the close button is not clicked
 send...
 sleep..
wend

Then it would respond to the window being closed, except that there would be a big delay sometimes because the script is sleeping so much, and it still doesn't respond the clicks on other things.

This means that you need a different approach for your send bits. One method that helps in this situation is the way of responding to events like a click on a button by using a mode of operation for the script called GuiEventMode. Since I assume you are just starting with all this then I suggest you keep that in mind but leave it for the moment and understand ways using the GuiGetMsg approach which you are already using.

So what's needed is something like this

While 1
 
 $nMsg = GUIGetMsg()
 Switch $nMsg
 Case $GUI_EVENT_CLOSE
 Exit

 Case $Button1
 ;set up something which sends a line of text every 2 seconds
 ;but lets the script carry on and do other things
 
 Case $Button2
 ;set up something which sends a line of text every 32 seconds
 ;but lets the script carry on and do other things

 EndSwitch
 WEnd

There is a way of dealing with that sort of thing built into AutoIt. Look up AdlibRegister. You can also use timers (look up _Timer_setTimer) but AdlibRegister is the best way to start I think.

If you get stuck then, as you see, there are willing helpers here.


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
XTensionX

Well that does make it endless... That and the sleep for 32 seconds...

Yes..ish.

When you press a button your script goes into the while/wend loop and stays there going round and round. In the loops the only thing the script is doing is what you tell it to do, ie send a string to NotePad and wait for 2 seconds or 32 seconds. To respond to anything it needs to be looking out for other things. If someone presses a button then the script will only find out by seeing what the result of GuiGetMsg is, so you you could make your loop change from

while 1
 send...
sleep...
Wend

to

while GuiGetMsg <> -3 ;while the close button is not clicked
 send...
 sleep..
wend

Then it would respond to the window being closed, except that there would be a big delay sometimes because the script is sleeping so much, and it still doesn't respond the clicks on other things.

This means that you need a different approach for your send bits. One method that helps in this situation is the way of responding to events like a click on a button by using a mode of operation for the script called GuiEventMode. Since I assume you are just starting with all this then I suggest you keep that in mind but leave it for the moment and understand ways using the GuiGetMsg approach which you are already using.

So what's needed is something like this

While 1
 
 $nMsg = GUIGetMsg()
 Switch $nMsg
 Case $GUI_EVENT_CLOSE
 Exit

 Case $Button1
 ;set up something which sends a line of text every 2 seconds
 ;but lets the script carry on and do other things
 
 Case $Button2
 ;set up something which sends a line of text every 32 seconds
 ;but lets the script carry on and do other things

 EndSwitch
 WEnd

There is a way of dealing with that sort of thing built into AutoIt. Look up AdlibRegister. You can also use timers (look up _Timer_setTimer) but AdlibRegister is the best way to start I think.

If you get stuck then, as you see, there are willing helpers here.

Also in these situations there is good approach to replace Sleep() command by TimerInit()/TimerDiff()

to be possible to cancel desired waiting cycles.

thanks for the help guys

i wanted my button to continuosly sending some test to a control,

i tried replacing

while 1
ControlSend("[CLASS:Notepad]", "", "Edit1", "line ")
Sleep (2000)
wend

to

while GuiGetMsg <> -3
ControlSend("[CLASS:Notepad]", "", "Edit1", "line ")
Sleep (2000)
wend

but the GUI closes itself after I press Button 1 and that wouldn't send the text repeatedly

is it possible to make Button 1 sends a text to a control continuosly every 2 seconds and having my Button 2 be able to do other things while button 1 is sending the text?

thanks for any hep guys, i really appreciate it

Share this post


Link to post
Share on other sites
BrettF

Call it fix the errors or something.

$SENDIT = False
$timer = TimerInit
While 1
    If ($SENDIT = TRUE) AND (TimerDiff ($timer) >= 2000) Then
        Send stuff
    EndIf

    Switch GUIGetMsg ()
        Case -3
            Exit
        Case $button1
            $SENDIT = True
    EndSwitch
WEnd

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.