Jump to content

Recommended Posts

Posted (edited)

Hello again.

The title says it all, I've got this script that starts when I press a button. I also have this other button that is supposed to stop the script and exit the program, however, as soon as I press the start button, the Stop button won't work. If I on the other hand press the Stop button before the script runs, the program exits directly.

I'm quite sure I know why this happens, but I have no clue how to fix this.

My thoughts would be something like.

--------------------------------------------------------------

If $button2 = isClicked <-- (I know, no such command) then

Exit 0

endif

--------------------------------------------------------------

But can't seem to find that command.

Here is the code atleast..

#include <NomadMemory.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("IP ftw", 625, 443, 192, 124)
$Label1 = GUICtrlCreateLabel(" ", 8, 32, 84, 25)
$Label2 = GUICtrlCreateLabel("", 8, 104, 84, 33)
$Label3 = GUICtrlCreateLabel("", 8, 176, 84, 42)
$Button1 = GUICtrlCreateButton("Start", 8, 360, 257, 57, $WS_GROUP)
$Button2 = GUICtrlCreateButton("Stop", 300, 360, 257, 57, $WS_GROUP)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###


HotKeySet("{F7}", "_terexit")
Func _terexit()
    Exit 0
EndFunc   ;==>_terexit

Global $Stop = 1


Func _start()
    Global $1 = 1
    Global $2 = 1
    Global $3 = 1

    If WinActive("Google Chrome") Then
    Else
        MsgBox(0, "", "Process was not found.")
    EndIf
    WinWaitActive("Google Chrome") ;Start your chrome.
    $Start = 1
    If $Start = 1 Then
        Sleep(2000)

        Send("{TAB 11}")
        Sleep(3500)
        Send("{Enter}")
        Sleep(12000)
        Send("******")
        Sleep(200)
        Send("{Enter}")
        $Start += 1
    EndIf

    WinWaitActive("NetClient") 

    While $1 = 1
        $startFind = PixelSearch(321, 142, 1597, 937, 0x900000)
        If IsArray($startFind) = True Then
            MouseMove($startFind[0], $startFind[1], 5)
            Sleep(9000)
            MouseClick("left")
            Sleep(1000)
            $1 += 1
        EndIf
    WEnd

    While $2 = 1
        $Custom = PixelSearch(321, 142, 1597, 937, 0xBFBF99)
        If IsArray($Custom) = True Then
            MouseMove($Custom[1], $Custom[1], 5)
            Sleep(1000)
            MouseClick("left")
            Sleep(1000)
            $2 += 1
        EndIf
    WEnd

    MouseMove(602, 866, 5)
    Sleep(500)
    MouseClick("left")
    Sleep(1000)
    Send("epic")
    Send("{Tab}")
    Send("fist")
    MouseMove(544, 873, 5)
    Sleep(500)
    MouseClick("left")
    Sleep(500)
    MouseMove(783, 598, 5) 
    Sleep(1000)
    MouseClick("left")
    Sleep(1000)
    MouseClick("left")
    Sleep(1000)
    MouseClick("left")
    Sleep(1000)
    MouseClick("left")
    MouseMove(1255, 596, 5)
    Sleep(1000)
    MouseClick("left")
    Sleep(1000)
    MouseClick("left")
    Sleep(1000)
    MouseClick("left")
    Sleep(1000)
    MouseClick("left")
    Sleep(1000)
    MouseClick("left")
    Sleep(1000)
    MouseMove(1175, 635, 5)
    MouseClick("left")
    Sleep(5000)
    MouseMove(763, 419, 5)
    Sleep(1500)
    MouseClick("left")
    Sleep(300)
    MouseMove(1189, 646, 5)
    Sleep(1000)
    MouseClick("left")
    Sleep(40000)


    While $3 = 1
        $Lot = PixelSearch(1388, 698, 1582, 885, 0xA50FB4)
        If IsArray($Lot) = True Then
            MouseMove($Lot[0], $Lot[1], 5)
            MouseClick("right")
        EndIf
    WEnd
EndFunc   ;==>_start

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            _start()
        Case $Button2           
            Exit 0
    EndSwitch
WEnd

Thanks in advance :x

Edited by cxzzD
  • Moderators
Posted

cxzzD,

The Interrupting a running function tutorial in the Wiki will explain how to do it. :x

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

Posted

I found that tutorial quite hard to follow. Does anyone know an easier way of doing this or a more clear tutorial?

My head began spining around when I saw all that code (They've used 3 different stop things in the same code), I couldn't figure out what part of the code did what..

  • Moderators
Posted

cxzzD,

As I wrote the tutorial I am disappointed that you found it hard to follow - perhaps I need to rewrite it. :P

This is the example code reduced to just what is needed for your case. You are using lots of Sleep lines during which your script is completely inactive. So use the _Waiter function as in the following script instead - it looks for the interruption and you will be able to break out of your function just like the test one here:

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

; Declare a flag
$fInterrupt = 0

$hGUI = GUICreate("Test", 500, 500)

$hButton_1 = GUICtrlCreateButton("Start", 10, 10, 80, 30)
$hButton_2 = GUICtrlCreateButton("End", 10, 50, 80, 30)

GUISetState()

; Intercept Windows command messages with out own handler
GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hButton_1
            _Func_1()
    EndSwitch
WEnd

Func _Func_1()
    ; Make sure the flag is cleared
    $fInterrupt = 0
    For $i = 1 To 20

        ; Show we are running
        ConsoleWrite("-Running" & @CRLF)

        ; Use this instead of Sleep as it looks for the flag rather than being inactive
        _Waiter(100)

    Next
    ; Ended normally
    ConsoleWrite(">Ended" & @CRLF)
EndFunc

Func _Waiter($iDelay)

    $iBegin = TimerInit()
    ; Start a loop for the required time
    Do
        Sleep(10)
        ; And check if the flag is set
        If $fInterrupt Then
            ; If it is then exit
            ConsoleWrite("!Interrrupted" & @CRLF)
            Exit
        EndIf
    Until TimerDiff($iBegin) > $iDelay
    Return 0

EndFunc

Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
    ; The Exit button was pressed so set the flag
    If BitAND($wParam, 0x0000FFFF) =  $hButton_2 Then $fInterrupt = 1
    Return $GUI_RUNDEFMSG
EndFunc   ;==>_WM_COMMAND

Is that easier to understand? Please ask if you have any questions - the idea is that you get your script to run as you wish. :x

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

Posted
  Quote

As I wrote the tutorial I am disappointed that you found it hard to follow - perhaps I need to rewrite it. :x

No, I think its very understandable for the Novice and Advanced user. This is how I learnt about WM_COMMAND!

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

  On 1/13/2011 at 7:48 PM, 'Melba23 said:

cxzzD,

As I wrote the tutorial I am disappointed that you found it hard to follow - perhaps I need to rewrite it. :shifty:

This is the example code reduced to just what is needed for your case. You are using lots of Sleep lines during which your script is completely inactive. So use the _Waiter function as in the following script instead - it looks for the interruption and you will be able to break out of your function just like the test one here:

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

; Declare a flag
$fInterrupt = 0

$hGUI = GUICreate("Test", 500, 500)

$hButton_1 = GUICtrlCreateButton("Start", 10, 10, 80, 30)
$hButton_2 = GUICtrlCreateButton("End", 10, 50, 80, 30)

GUISetState()

; Intercept Windows command messages with out own handler
GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hButton_1
            _Func_1()
    EndSwitch
WEnd

Func _Func_1()
    ; Make sure the flag is cleared
    $fInterrupt = 0
    For $i = 1 To 20

        ; Show we are running
        ConsoleWrite("-Running" & @CRLF)

        ; Use this instead of Sleep as it looks for the flag rather than being inactive
        _Waiter(100)

    Next
    ; Ended normally
    ConsoleWrite(">Ended" & @CRLF)
EndFunc

Func _Waiter($iDelay)

    $iBegin = TimerInit()
    ; Start a loop for the required time
    Do
        Sleep(10)
        ; And check if the flag is set
        If $fInterrupt Then
            ; If it is then exit
            ConsoleWrite("!Interrrupted" & @CRLF)
            Exit
        EndIf
    Until TimerDiff($iBegin) > $iDelay
    Return 0

EndFunc

Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
    ; The Exit button was pressed so set the flag
    If BitAND($wParam, 0x0000FFFF) =  $hButton_2 Then $fInterrupt = 1
    Return $GUI_RUNDEFMSG
EndFunc   ;==>_WM_COMMAND

Is that easier to understand? Please ask if you have any questions - the idea is that you get your script to run as you wish. :x

M23

I think ill try to understand the more basic things before I try this again. Seems like im too much of a dumbass to get this.

Thanks for your time though, really appreciate it.

Any ideas for simple tasks to do?

I remember when I was learning c# that there was a couple of websites dedicated to create simple tasks that you were supposed to finish to move on, anything similar? :P

Posted

  Quote

Any ideas for simple tasks to do?

I remember when I was learning c# that there was a couple of websites dedicated to create simple tasks that you were supposed to finish to move on, anything similar? :x

I would suggest looking at C:\Program Files\AutoIt3\Examples to get an idea of simple examples.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

  • Moderators
Posted

cxzzD,

I was serious when I said that I wanted to make sure you could code as you wanted. What is about the code I posted that makes it hard to understand? I am happy to explain any of it in more detail if required. :x

guinness,

Thanks you for the compliment - I did try to make the tutorial understandable. :P

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

Posted

  On 1/13/2011 at 8:24 PM, 'cxzzD said:

I remember when I was learning c# that there was a couple of websites dedicated to create simple tasks that you were supposed to finish to move on, anything similar? :x

Try this -

- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

Posted

I stumbled onto that thing more than once. No matter how people say it's easy. It isn't. Melba23 did wonderful explanations in this I made. I didn't got it to work and I didn't understood too, help it isn't the same for you. Anyway, I'm planning to learn it.

A friend of mine shown this solution, which is really EASY to understand:

Just use GuiOnEventMode to do your gui and all will work fine.

A little example:

Opt("GUIOnEventMode", 1)

HotKeySet("{F1}", "_Start")
HotKeySet("{F2}", "_Pause")
HotKeySet("{F3}", "_Exit")

$IsPaused=1

$GUI=GUICreate("test", 300, 40)
GUISetOnEvent(-3, "_Exit")
GUICtrlCreateButton("Start", 10, 10, 80, 17)
GUICtrlSetOnEvent(-1, "_Start")
GUICtrlCreateButton("Pause", 110, 10, 80, 17)
GUICtrlSetOnEvent(-1, "_Pause")
GUICtrlCreateButton("Exit", 210, 10, 80, 17)
GUICtrlSetOnEvent(-1, "_Exit")
GUISetState()

Do
    Sleep(100)
    If $IsPaused=-1 Then
        ConsoleWrite(@HOUR&":"&@MIN&":"&@SEC&@TAB&"NotPaused"&@CRLF)
    EndIf
Until False

Func _Start()
    $IsPaused=-1
EndFunc

Func _Pause()
    $IsPaused=1
EndFunc

Func _Exit()
    Exit
EndFunc

Try it :x

I'm a compulsive poster. When I post something, come to read it at least 5 minutes later after the posting, because I will edit it. I edited even this signature a few minutes later after I wrote it.

Posted

  On 1/13/2011 at 8:25 PM, 'guinness said:

I would suggest looking at C:\Program Files\AutoIt3\Examples to get an idea of simple examples.

Those things were abit to easy! Haha :x

  • Developers
Posted (edited)

  On 1/13/2011 at 3:14 PM, 'cxzzD said:

Hello again.

I see this little piece of shit came back ... so you even can't keep your word and think by just changing the displayname the issues is resolved.

Remember? :

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Posted

  On 1/13/2011 at 8:55 PM, 'Melba23 said:

cxzzD,

I was serious when I said that I wanted to make sure you could code as you wanted. What is about the code I posted that makes it hard to understand? I am happy to explain any of it in more detail if required. :x

guinness,

Thanks you for the compliment - I did try to make the tutorial understandable. :shifty:

M23

Yea, I didn't mean to bash your tutorial or anything. Just me having a hard time understanding(as allways..).

I might give this another shot tomorrow. But I gotta say, my program, atleast if I ever get to finishing it, will need somekind of multithreading. And if I've understand it correctly, AutoIt dosn't support multithreading.

Is there any other brilliant way that makes it possible to run several loops at once? :P

*

Posted

  On 1/13/2011 at 9:01 PM, 'somdcomputerguy said:

Try this -

I've actually gone thru that one and I have to say it was ridiculously easy imo. :x

Posted

  On 1/13/2011 at 9:14 PM, 'Newb said:

I stumbled onto that thing more than once. No matter how people say it's easy. It isn't. Melba23 did wonderful explanations in this I made. I didn't got it to work and I didn't understood too, help it isn't the same for you. Anyway, I'm planning to learn it.

A friend of mine shown this solution, which is really EASY to understand:

Just use GuiOnEventMode to do your gui and all will work fine.

A little example:

Opt("GUIOnEventMode", 1)

HotKeySet("{F1}", "_Start")
HotKeySet("{F2}", "_Pause")
HotKeySet("{F3}", "_Exit")

$IsPaused=1

$GUI=GUICreate("test", 300, 40)
GUISetOnEvent(-3, "_Exit")
GUICtrlCreateButton("Start", 10, 10, 80, 17)
GUICtrlSetOnEvent(-1, "_Start")
GUICtrlCreateButton("Pause", 110, 10, 80, 17)
GUICtrlSetOnEvent(-1, "_Pause")
GUICtrlCreateButton("Exit", 210, 10, 80, 17)
GUICtrlSetOnEvent(-1, "_Exit")
GUISetState()

Do
    Sleep(100)
    If $IsPaused=-1 Then
        ConsoleWrite(@HOUR&":"&@MIN&":"&@SEC&@TAB&"NotPaused"&@CRLF)
    EndIf
Until False

Func _Start()
    $IsPaused=-1
EndFunc

Func _Pause()
    $IsPaused=1
EndFunc

Func _Exit()
    Exit
EndFunc

Try it :x

I have actually allready gotten a HotKeySet in my script. But that dosn't really cut it for me, I'd like my button to work. :P
Posted

  On 1/13/2011 at 10:34 PM, 'cxzzD said:

I have actually allready gotten a HotKeySet in my script. But that dosn't really cut it for me, I'd like my button to work. :lol:

If you at least tried to run the example :x:P:shifty:

You would have noticed that also buttons in the gui modality i told you do the thing you're asking for :nuke:

I'm a compulsive poster. When I post something, come to read it at least 5 minutes later after the posting, because I will edit it. I edited even this signature a few minutes later after I wrote it.

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...