Jump to content

Why cant start new function while one is already running?


Recommended Posts

So the best example

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 263, 168, 192, 124)
$Button1 = GUICtrlCreateButton("Button1", 8, 8, 75, 33)
$Button2 = GUICtrlCreateButton("Button2", 96, 9, 75, 31)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
  Case $GUI_EVENT_CLOSE
   Exit
  Case $Button1
   while 1
    sleep(200)
    Wend
   Case $Button2
    MsgBox(0,"What the hell?","Why this wont pop up?")
EndSwitch
WEnd

When you hit Button1 it starts function and while that one is running its impossible to open msgbox by hitting button2. Why? And its possible to change it?

Oh, maybe change switch or something? Im sure that its possible!

Edited by Edgaras
Link to comment
Share on other sites

I want to run function while one function is already running. Hmm I used info from that wiki to stop functions. So you mean I have to stop already running function to open that msg box and then continue main function? Can you write me some short code? Im really lost in that interrupting running function.

Link to comment
Share on other sites

  • Moderators

Edgaras,

Can you write me some short code?

As you can see from the Wiki tutorial it is not easy to interrupt a running function. But in the script example you have posted you are not running a function. It is quite possible to do what you want very easily - it took me about 30 secs to code it. :)

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

$Form1 = GUICreate("Form1", 263, 168, 192, 124)
$Button1 = GUICtrlCreateButton("Button1", 8, 8, 75, 33)
$Button2 = GUICtrlCreateButton("Button2", 96, 9, 75, 31)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            ConsoleWrite("Running" & @CRLF)
            While 1
                Sleep(10)
                $nMsg = GUIGetMsg()
                Switch $nMsg
                    Case $GUI_EVENT_CLOSE
                        Exit
                    Case $Button2
                        MsgBox(0, "What the hell?", "Why this now pops up!")
                EndSwitch
            WEnd
    EndSwitch
WEnd

All clear now? Or was that not what you are really trying to do? ;)

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:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

Edgaras,

So the best example

Whereas the example was actually nothing like the code you want to use. ;)

If you want help then please make sure that the code you provide is as close as possible to what you really want - otherwise we all waste a lot of time posting solutions which do not meet your requirements. :)

This is how you can interrupt your function and then let it continue:

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

Global $fInterrupt = False

$Form1 = GUICreate("Form1", 263, 168, 192, 124)
$Button1 = GUICtrlCreateButton("Button1", 8, 8, 75, 33)
$Button2 = GUICtrlCreateButton("Button2", 96, 9, 75, 31)
GUISetState(@SW_SHOW)

GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            _Run_Func_1()
    EndSwitch
WEnd

Func _Run_Func_1()
    ConsoleWrite("Running Func_1" & @CRLF)
    While 1
        Sleep(10)
        ; You will have to run something like this at regular intervals in your function
        If $fInterrupt Then
            $fInterrupt = False
            _Run_Func_2()
        EndIf
    WEnd
EndFunc

Func _Run_Func_2()
    ConsoleWrite("Pausing Func_1" & @CRLF)
    ConsoleWrite("Running Func_2" & @CRLF)
    Sleep(1000)
    ConsoleWrite("Continuing Func_1" & @CRLF)
EndFunc


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

If your function does not offer regular opportunities to check for the interrupt flag then you are going to find it very difficult to break into the running function and you may have to rethink your code. :)

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:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

"we all waste a lot of time posting solutions which do not meet your requirements." all codes is useful for me. In pc I have folders where I put examples and all codes is very usefull.

Ok, I got what I want. But now i dont understand ConsoleWrite. It only writes some text to show me smthg or this is important to code? I can delete it if i dont need, right?

Link to comment
Share on other sites

  • Moderators

Edgaras,

all codes is useful for me

A rather selfish attitude if I may say so - working on something for you which is not directly relevant to your problem means that someone else might not be getting their question resolved. So please do make your code as close as possible to what you really want - that way we can give you the correct answer as soon as possible. :)

The ConsoleWrite lines are only there to show you what is going on - you have no need to use them in your final script. ;)

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:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

Edgaras,

So in same way I can stop func just need edit _Run_Func_2?

I am not sure what you mean. If you want to stop the Func_1 function then you just use Return - there is no need for the second function at all:

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

Global $fInterrupt = False

$Form1 = GUICreate("Form1", 263, 168, 192, 124)
$Button1 = GUICtrlCreateButton("Button1", 8, 8, 75, 33)
$Button2 = GUICtrlCreateButton("Button2", 96, 9, 75, 31)
GUISetState(@SW_SHOW)

GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            _Run_Func_1()
    EndSwitch
WEnd

Func _Run_Func_1()
    ConsoleWrite("Running Func_1" & @CRLF)
    While 1
        Sleep(10)
        If $fInterrupt Then
            $fInterrupt = False
            ConsoleWrite("Stopping Func_1" & @CRLF)
            Return
        EndIf
    WEnd
EndFunc

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

All clear? ;)

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:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

Edgaras,

Just intercept the closure message like this: :)

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

Global $fInterrupt = 0

$Form1 = GUICreate("Form1", 263, 168, 192, 124)
$Button1 = GUICtrlCreateButton("Button1", 8, 8, 75, 33)
$Button2 = GUICtrlCreateButton("Button2", 96, 9, 75, 31)
GUISetState(@SW_SHOW)

GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")
GUIRegisterMsg($WM_SYSCOMMAND, "_WM_SYSCOMMAND")

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            _Run_Func_1()
    EndSwitch
WEnd

Func _Run_Func_1()
    ConsoleWrite("Running Func_1" & @CRLF)
    While 1
        Sleep(10)
        If $fInterrupt Then
            Switch $fInterrupt
                Case 1
                    ConsoleWrite("Stopping Func_1" & @CRLF)
                    $fInterrupt = 0
                    Return
                Case 2
                    ConsoleWrite("Exiting directly" & @CRLF)
                    Exit
            EndSwitch
        EndIf
    WEnd
EndFunc

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

Func _WM_SYSCOMMAND($hWnd, $msg, $wParam, $lParam)
    ; Closure [X} pressed
    If BitAND($wParam, 0xFFF0) = 0xF060 Then ; $SC_CLOSE
        $fInterrupt = 2
        ConsoleWrite("WM_Close intercepted" & @CRLF)
    EndIf
    Return $GUI_RUNDEFMSG
EndFunc   ;==>On_WM_SYSCOMMAND

All clear? ;)

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:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Thanks it works. But now im stucked on adding some code

Func myfunc()
_GUICtrlButton_Enable($Button4, False)
    ConsoleWrite("Running Func_1" & @CRLF)
    While 1
  ;;;;;;;;;;;;code;;;;;;;;;;;;;;;;;;code;;;;;;;;;;;;;;;;;;;code;;;;;;;;;;;;;;;;code;;;;;;;;;;;;;;;;code;;;;;;;;;
   $body = _IEBodyReadHTML($oIE)
  Sleep(4500)
_IEAction($oIE, "refresh")
If StringInStr($body, "Some text") Then
  MsgBox(0,"Error","No match")
  If StringInStr($body, "Some text") Then
  MsgBox(0,"Error","Also No match")
         ; how I can do some if here? Every time i try then it works but then I cant use $fInterrupt button or [X] button. Should I make this with cases or what?   Also how I should stop function? Hmm start interrupt func.

If StringInStr($body, "Some text") Then
  MsgBox(0,"Error","Also No match")
;----------><----------- here?  Something like $fInterrupt = 0?
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;code;;;;;;;;;;;;;;;;;;;code;;;;;;;;;;;;;;;;;;;code;;;;;;;;;;;;;;;;;code;;;;;;;;;;;;;;
       If $fInterrupt Then
             Switch $fInterrupt
                Case 1
                    $fInterrupt = 0
                    Return
                Case 2
                    Exit
            EndSwitch
        EndIf
    WEnd
EndFunc
Link to comment
Share on other sites

  • Moderators

Edgaras,

Perhaps something like this will work for you - obviously I have not tested it: ;)

Func myfunc()
    _GUICtrlButton_Enable($Button4, False)
    ConsoleWrite("Running Func_1" & @CRLF)
    While 1
        ;;;;;;;;;;;;code;;;;;;;;;;;;;;;;;;code;;;;;;;;;;;;;;;;;;;code;;;;;;;;;;;;;;;;code;;;;;;;;;;;;;;;;code;;;;;;;;;
        $body = _IEBodyReadHTML($oIE)
        
        ; Use this 4.5 sec delay to look for the flag
        Local $iBegin = TimerInit() ; Set a timestamp
        Do
            Sleep(10)
            If $fInterrupt Then ; Check for the flag
                Switch $fInterrupt
                    Case 1
                        $fInterrupt = 0
                        Return
                    Case 2
                        Exit
                EndSwitch
            EndIf
        Until TimerDiff($iBegin) > 4500 ; Loop until the delay is ended
        
        ; Now do the checks on the HTML
        _IEAction($oIE, "refresh")
        If StringInStr($body, "Some text ") Then
            MsgBox(0, "Error", "No match")
            If StringInStr($body, "Some text") Then
                MsgBox(0, "Error", "Also No match")
                If StringInStr($body, "Some text") Then
                    MsgBox(0, "Error", "Also No match")
                EndIf
            EndIf
        EndIf
    WEnd
EndFunc   ;==>myfunc

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:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

I cant understand half of these things. Anyway im going to study autoit for a while as I finished exams ;)

Also how I can stop func if string is not found?

It would stop automatically without pushing any button.

$fInterrupt = True ???

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