Sign in to follow this  
Followers 0
pecas

How to "send" an ExitLoop from outside the script

4 posts in this topic

I use a script similar to this:

While 1
    If Not (my conditions are true) Then
        Sleep(1000)
    Else
        While 1
            (My Code Here)
            If (timer expired) Then
                ExitLoop
            EndIf
        WEnd
    EndIf
WEnd

The script is made to run 24/7, thus the main infinite loop (I have a Hotkeyset to stop it).

The second loop inside repeats the code until a timer expires, and then Exits to the main loop again.

Now my problem: once in a while, the script gets stuck inside the secondary loop. This depends on reasons outside the script that can't be changed. I found a suitable solution to terminate the script through another script, which checks the conditions, and smoothly terminates everything. But since I want my main script to run 24/7, I would love to have the ability to send from outside an "ExitLoop" command, so that it would restart from the main loop.

Unfortunately, you can't use ExitLoop outside of a loop, and this prevents me to use a HotkeySet, or GUI event which refer to an external Function like this:

Func _ExitLoop
    ExitLoop
EndFunc

Suggestions?

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Unfortunately, you can't use ExitLoop outside of a loop, and this prevents me to use a HotkeySet, or GUI event which refer to an external Function like this:

Func _ExitLoop
    ExitLoop
EndFunc
Nothing prevents you from doing this:

Global $fExitLoop

While 1
    $fExitLoop = 0
    If Not (my conditions are true) Then
        Sleep(1000)
    Else
        While 1
            (My Code Here)
            If (timer expired) Or $fExitLoop Then
                ExitLoop
            EndIf
        WEnd
    EndIf
WEnd

Func _ExitLoop()
    $fExitLoop = True
EndFunc

And how you trigger that _ExitLoop is up to you.

For example, if you really have to do it from another script, register a message in your main app

$hGui = GUICreate("Main app", 100, 100)
GUIRegisterMsg(0x8123, '_ExitLoop');$WM_APP+somenumber

and then in that other script, do

$hWnd = WinGetHandle("Main app")
If Not @error Then DllCall('user32.dll', 'int', 'PostMessage', 'hwnd', $hWnd, 'uint', 0x8123, 'wparam', 0, 'lparam', 0)
Edited by Siao

"be smart, drink your wine"

Share this post


Link to post
Share on other sites

Nothing prevents you from doing this:

Global $fExitLoop

While 1
    $fExitLoop = 0
    If Not (my conditions are true) Then
        Sleep(1000)
    Else
        While 1
            (My Code Here)
            If (timer expired) Or $fExitLoop Then
                ExitLoop
            EndIf
        WEnd
    EndIf
WEnd

Func _ExitLoop()
    $fExitLoop = True
EndFunc
This won't solve my problem, because my main script gets stuck in (My Code Here) part... when it gets regularly to the If, (timer expired) works correctly, so there's no need to force an ExitLoop.

I'll try your second suggestion, as soon as I have time today. I never used DllCall function, so will have to do some homework first. Will let you know if it solved my issue.

Many thanks for your answer.

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  
Followers 0