Jump to content
akshatgupta

issue because of single thread

Recommended Posts

akshatgupta

i was working on a small project which uses GUI

when i run the program it is supposed to run in an infinite loop

but i want it to quit running when i press the stop button but the button does not work because the program is single thread

any ideas on what to do?

Share this post


Link to post
Share on other sites
orbs

not wishing to be discouraging, but your "issue" has nothing to do with single/multi-threading.

i advise you begin by reading, running and understanding the help file example for GUICreate(). it is very easy to make a GUI program do stuff in the background while responding to the GUI.

if you want further help with a specific issue, describe it in more relevant details, and preferably post a reproducer (a small functional script that demonstrates the issue).

Share this post


Link to post
Share on other sites
akshatgupta
Posted (edited)

sorry but i am a bit new to autoit and i just saw the help file and not having a clue what could be wrong

here is my gui code

$Form1 = GUICreate("Form1", 312, 157, 996, 422)
$status = GUICtrlCreateLabel("Status: NO ACTIVITY", 0, 136, 307, 17, $SS_CENTER)
$start = GUICtrlCreateButton("START", 88, 16, 139, 41)
GUICtrlSetFont(-1, 15, 400, 0, "MS Sans Serif")
$stop = GUICtrlCreateButton("STOP", 88, 72, 139, 41)
GUICtrlSetFont(-1, 15, 400, 0, "MS Sans Serif")
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $start
            AlignWindow()
            StartLoop()
        Case $stop
            Exit
    EndSwitch
WEnd

i used koda for gui creation

Edited by akshatgupta

Share this post


Link to post
Share on other sites
Andreik

You missed the most important part, functions AlignWindow() and StartLoop(). Probably you have there loops where you didn't check for new GUI messages, am I right?


When the words fail... music speaks

Share this post


Link to post
Share on other sites
orbs
Posted (edited)

first comes to mind: why do you have two buttons? if you click "START", and the process starts, what do you expect to happen if you click "START" again? isn't it more appropriate to have one button to start or stop the process?

(i know it sounds diverting from the topic, but trust me on this - if you build your GUI in a way most suitable to your needs, you 'll find the rest of the coding much easier).

EDIT: @Andreik is of course right, but it;s not the process functions that should be checking for GUI messages. i'd expect a global variable like $bRunning that is controlled by the GUI, and being checked by the process functions. or yet more simple, if the process function is a simple loop, it can be embedded into the main loop of the script.

EDIT #2: regardless, GUI OnEvent mode may be better suited.

 

Edited by orbs

Share this post


Link to post
Share on other sites
akshatgupta
Just now, orbs said:

first comes to mind: why do you have two buttons? if you click "START", and the process starts, what do you expect to happen if you click "START" again? isn't it more appropriate to have one button to start or stop the process?

(i know it sounds diverting from the topic, but trust me on this - if you build your GUI in a way most suitable to your needs, you 'll find the rest of the coding much easier).

 

 

when start is clicked the the start button is disabled so you cant click it anymore though i never tried doing that

 

Share this post


Link to post
Share on other sites
akshatgupta
5 minutes ago, Andreik said:

You missed the most important part, functions AlignWindow() and StartLoop(). Probably you have there loops where you didn't check for new GUI messages, am I right?

yea that might be the issue thanks :)

 

Share this post


Link to post
Share on other sites
Bilgus

If you get to a point where you just can't do without an asynchronous process for your loops your best bet is to move it out to another script and run that rather than even trying multi threading -- that will only result in a headache at best

That being said you could simulate something close like this:

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

$Form1 = GUICreate("Form1", 312, 157, 996, 422)
Global $iNumbers = 1
Global $dummy = GUICtrlCreateDummy()

$status = GUICtrlCreateLabel("Status: NO ACTIVITY", 0, 136, 307, 17, $SS_CENTER)
$start = GUICtrlCreateButton("START", 88, 16, 139, 41)
GUICtrlSetFont(-1, 15, 400, 0, "MS Sans Serif")
$stop = GUICtrlCreateButton("STOP", 88, 72, 139, 41)
GUICtrlSetFont(-1, 15, 400, 0, "MS Sans Serif")
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $start
            AlignWindow()
            StartLoop()
        Case $dummy
            Count()
        Case $stop
            Exit
    EndSwitch
WEnd

Func AlignWindow()
EndFunc   ;==>AlignWindow

Func StartLoop()
    ;set up the conditions for your loop here
    $iNumbers = 100
    GUICtrlSendToDummy($dummy, 0) ; (you could also send messages)
EndFunc

Func Count()
    For $i = 1 To 100
        ;Do 'some' work
        $iNumbers += 1
    Next
    GUICtrlSendToDummy($dummy, 0) ; (you could also send messages)
    GUICtrlSetData($status, "Status: Activity (" & $iNumbers & ")")
EndFunc

 

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

×