Sign in to follow this  
Followers 0
Pain

stop function while running?

9 posts in this topic

#1 ·  Posted (edited)

Is it possible to stop a function while it is running/executing?

I got some big functions but I need a "emergency button" if you see that something is wrong because my script reads settings from a ini-file, so a minor error in the ini-file could have a devastating effect.

At the moment I'm using the "Pause Script" and "Exit" from the trayicon and they work the way they should (and the way I want them to work).

The thing is that I want a button to do the same thing because my script is working in a fullscreen program so having to tab out from the program and click on the trayicon would take some time.

Therefore I'm using a TopMost GUI with a button to stop it as soon as you see that something is wrong.

I've tried several things like ProcessClose, Exit but it will only wait until the function has ended and then exit. ExitLoop won't work since it's not in a loop.

HotkeySet is NOT possible in my script because of the same reason as mentioned above.

Here is a small piece of my code, I'm not sure if it will work 100% since I removed some of the bigger functions but hopefully it will be easier to understand atleast.

I also got some more minor questions that are written in the script as comments too, feel free to answer those too but the emergency stop has highest priority,

I can live with the rest of the errors.

#include <GUIConstants.au3>

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("test", 267, 276, 465, 283, -1, BitOR($WS_EX_TOOLWINDOW,$WS_EX_TOPMOST,$WS_EX_WINDOWEDGE))
$Button1 = GUICtrlCreateButton("Start", 75, 200, 100, 33, 0)
GUICtrlSetFont(-1, 8, 800, 0, "Verdana")
$Button3 = GUICtrlCreateButton("Emergency Stop", 70, 240, 120, 25, 0)
GUICtrlSetFont(-1, 8.5, 900, 0, "Verdana")
;GUICtrlSetColor, GUICtrlSetBkColor does not seems to work here, the button will looks like it's always pressed.

GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
     Select
         Case $nMsg = $GUI_EVENT_CLOSE
             ExitLoop ;or just Exit?
         Case $nMsg = $Button1
             start()
         Case $nMsg = $Button3
             kill()
         Case Else
     EndSelect
WEnd
Exit

Func kill()
    While ProcessExists(@ScriptName) 
        ProcessClose(@ScriptName)
        sleep(40)
    WEnd
    Exit
;~      If ProcessExists(@ScriptName) Then
;~      ProcessClose(@ScriptName)
EndFunc

Func start()
$Read = "hello world" ;ini-read but I changed it so it will work for testing too without the inifile

    ;unchecked = 4
    ;checked = 1
    
$Check = 1 ;force it to be 1 since I removed the checkbox by mistake.

    If $Check = 1 then
        sleep (5000)
        send("{Enter}")
        sleep(1000)
        send($Read)
        sleep(1000)
        send("{Enter}")
        sleep(100)
    EndIf
;~      function2()
EndFunc

Seems like I deleted some lines too much but "Check" is a checkbox that I read the state from but I forced it to work and "function2" is a function that looks similar to "start" (most sleeps and sends) but it's alot bigger (some hundred lines) so I removed it to keep it short.

To simulate my problem press on Start and while the start function is running click on the Stop button.

Edited by Pain

Share this post


Link to post
Share on other sites



#3 ·  Posted (edited)

I'm not sure why it won't work but the script are sending the text to a fullscreen program that has to be activated to work.

I've tried using hotkeyset but it seems like the key was send to the fullscreenprogram instead and nothing happended (if the key didn't had any other function in the program).

Maybe it should work if I first clicked on my GUI (to activate it) and then pressed the hotkeyset but I haven't tried yet.

Edited by Pain

Share this post


Link to post
Share on other sites

I'm not sure why it won't work but the script are sending the text to a fullscreen program that has to be activated to work.

I've tried using hotkeyset but it seems like the key was send to the fullscreenprogram instead and nothing happended (if the key didn't had any other function in the program).

Maybe it should work if I first clicked on my GUI (to activate it) and then pressed the hotkeyset but I haven't tried yet.

What is the HotKey you used and maybe more important: What program has the focus?

One other thing you could do is define an ADLIB function and let it check for the mouse position. You could let it test for position 0,0 so when the mouse is moved in the left top corner the script will stop.

Jos


Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

I were using ESC as HotkeySet, that key does not have any other function for the program.

The program is a game called knightonline (similar to WoW) where I'm a moderator/gamemaster.

Adlib might work as you said, I'll give it a try and see if it works. Thanks for the suggestion muttley

But then you should be able to call the adlib to see if the Stop button was pressed, right?

Edited by Pain

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Adlib might work as you said, I'll give it a try and see if it works. Thanks for the suggestion muttley

But then you should be able to call the adlib to see if the Stop button was pressed, right?

My idea was more to test the exact location of the mouse in the adlib function and act on that without having to do anything else. Edited by Jos

Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

You mean something like this?

Func mouse()
$pos = MouseGetPos()

$pos[0] = 0

$pos[1] = 0

While 1
    $Pos2 = MouseGetPos()
    If $Pos[0] = $Pos2[0] AND $Pos[1] = $Pos2[1] Then
        MsgBox(0,"","STOP")
    EndIf
    Sleep(5)
WEnd
EndFunc

If that is correct where should I add the AdLib because I tried with

AdlibEnable ("mouse", 1000)

at the beginning of my script but it didn't seems to work at all, I couldn't even close the script and the trayicon stopped working etc. so I had to close it from taskmanager before my computer would crash (it use to happen pretty often when I try my scripts muttley)

Share this post


Link to post
Share on other sites

I meant more something like this:

AdlibEnable ("mouse")
; Your stuff here
;
;
;
; Added for demo purpose
While 1
    Sleep(10)
WEnd
;
;
Func mouse()
    $pos = MouseGetPos()
    If $pos[0] < 5 And $pos[1] < 5 Then
        MsgBox(0, "", "Stopping script",5)
        Exit
    EndIf
EndFunc  ;==>mouse

This will abandon your script when you move the mouse to the left top corner....

Jos


Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Thanks alot for your help, everything is working just fine and the way it should now muttley

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