Jump to content

issue because of single thread


Recommended Posts

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?

Link to comment
Share on other sites

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

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

 

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