Sign in to follow this  
Followers 0
Monolith

Run multiple instances of called function?

6 posts in this topic

#1 ·  Posted (edited)

Hello.

Is it possible to when calling a given function from a button within side a gui, to enable it to be run simultaneously once the given function has been called and an instance of the executed command exists?

The scope, i need to click a button which calls a function to leave an instance of notepad running while checking that instance. directly after the first initial activation of the button function i need to spawn another instance by clicking on the button once again so then 2x instances of notepad are running while the functions routine is checking each instance.

just for starters i will only need to allow for 2x simultaneous instances to occur.

Multi-threading workaround?

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Include <Constants.au3>

Opt("TrayMenuMode",1)
Opt("GUIOnEventMode", 1)

_gui1()

func _gui1()
$Form1 = GUICreate("Form1", 554, 362, 1045, 280)
$Pic1 = GUICtrlCreatePic("E:\Pics\157327742_Mtg6e-L.jpg", 0, 0, 553, 361, $WS_CLIPSIBLINGS) ;test location without fileinstall
$Label1 = GUICtrlCreateLabel("Install ", 16, 288, 124, 17)
GUICtrlSetCursor(-1, 0)
GUICtrlSetBkColor(Default, $GUI_BKCOLOR_TRANSPARENT)
GUICtrlSetColor(Default, 0xFFFFFF)
GUICtrlSetOnEvent(-1, "Installer")
GUISetOnEvent($GUI_EVENT_CLOSE, "Btnexit")
$Label2 = GUICtrlCreateLabel("notepad", 384, 288, 43, 17)
GUICtrlSetOnEvent(-1, "notepad")
GUICtrlSetCursor(-1, 0)
GUICtrlSetBkColor(Default, $GUI_BKCOLOR_TRANSPARENT)
GUICtrlSetColor(Default, 0xFFFFFF)

GUISetState(@SW_SHOW)
    Do
        $msg = GUIGetMsg()
    Until $msg = $GUI_EVENT_CLOSE
EndFunc


Func Btnexit() ;exits application with a message box
    Exit
EndFunc   ;==>Btnexit


Func Installer()
        MsgBox(0, '', 'Installer started', 4)
        TrayTip("Copying", "copying installer to" & ' ' & "", 5, 1)
EndFunc

func notepad()
    Run("notepad.exe")
WinWait("[CLASS:Notepad]")
Local $hWnd = WinGetHandle("[CLASS:Notepad]")
Local $sHWND = String($hWnd)
WinSetState(HWnd($sHWND), "", @SW_MINIMIZE)
sleep(2780)
WinSetState(HWnd($sHWND), "", @SW_RESTORE)
Sleep(7000)
WinClose(HWnd($sHWND))
EndFunc

thanks in advance ;)

Edited by Monolith

Share this post


Link to post
Share on other sites



If you just want to run more than one Notepad then you don't need anything very complicated I think. You need to identify each instance correctly though.

Local $hWnd = WinGetHandle("[CLASS:Notepad]") is not going to ensure that the handle is for the correct window. So use the return from Run to get the process ID then get the window handle from that.

If your Notepad function returns the window handle, or process ID, then whenever you want to do something like minimize or restore or close the notepad then all you need is a function to do that which is given the correct handle or id.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

If you just want to run more than one Notepad then you don't need anything very complicated I think. You need to identify each instance correctly though.

Local $hWnd = WinGetHandle("[CLASS:Notepad]") is not going to ensure that the handle is for the correct window. So use the return from Run to get the process ID then get the window handle from that.

If your Notepad function returns the window handle, or process ID, then whenever you want to do something like minimize or restore or close the notepad then all you need is a function to do that which is given the correct handle or id.

Hi Martin

Thanks for the reply, I understand what you are saying about getting the relative handle from $var = Run() and that's what i will be implementing but still yields the same issue when the single function is bit more complex.

One of the problems which i forgot to mention is that once the button is pressed and the function is called i am unable to press the button again and get an immediate response in the form of running the function again. when the button is pressed the second time it "queues" and waits for the first instance to complete then performs the queued action.

another explanation:

notepad button pressed twice within 2 seconds calling the same function but running simultaneously "side by side" No queue no waiting just BAM! execute :evil:

maybe another approach? say check if function is "locked" by first attempt, then when button is pressed the second time while the first function routine is being carried out then call for another function which will do exactly the same as the first function just contained in a different segment?

Forgive me but I don't really know any other way of articulating what i am trying to achieve ;) other than the two posts i have created in this thread.

I am using notepad as an example, my main goal is doing remote *)$nstalls^ on upto 2x target machines which will have allot more checking involved.

thanks

Edited by Monolith

Share this post


Link to post
Share on other sites

Hi Martin

Thanks for the reply, I understand what you are saying about getting the relative handle from $var = Run() and that's what i will be implementing but still yields the same issue when the single function is bit more complex.

One of the problems which i forgot to mention is that once the button is pressed and the function is called i am unable to press the button again and get an immediate response in the form of running the function again. when the button is pressed the second time it "queues" and waits for the first instance to complete then performs the queued action.

another explanation:

notepad button pressed twice within 2 seconds calling the same function but running simultaneously "side by side" No queue no waiting just BAM! execute :evil:

maybe another approach? say check if function is "locked" by first attempt, then when button is pressed the second time while the first function routine is being carried out then call for another function which will do exactly the same as the first function just contained in a different segment?

Forgive me but I don't really know any other way of articulating what i am trying to achieve ;) other than the two posts i have created in this thread.

I am using notepad as an example, my main goal is doing remote *)$nstalls^ on upto 2x target machines which will have allot more checking involved.

thanks

I think you need to return the handle or PID from the function as soon as possible and not have those sleeps. Then handle whatever you want to do with the window another way.

For example, set up an array which is something like this

Global $aHwins[10][5]; allow for 10 windows to start with, each window has 5 variables

;[$n][0] = window handle, [$n][1] = current state, [$n][2] = action required if any, [$n][3] the time to perform the action etc

when you get the return from Notepad set up the elements, or do it in Notepad.

Then have a function which sorts all these things out, say

AblibREgister("NPhandler",500)

Func NPhandler()
for $n = 0 to UBound($aHwins) - 1
 ;do whatever
 if $aHwins[$n][0] <> 0 and $aHwins[$n][2] <> 0
    ;blah blah
 endif
next

endfunc

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

I think you need to return the handle or PID from the function as soon as possible and not have those sleeps. Then handle whatever you want to do with the window another way.

For example, set up an array which is something like this

Global $aHwins[10][5]; allow for 10 windows to start with, each window has 5 variables

;[$n][0] = window handle, [$n][1] = current state, [$n][2] = action required if any, [$n][3] the time to perform the action etc

when you get the return from Notepad set up the elements, or do it in Notepad.

Then have a function which sorts all these things out, say

AblibREgister("NPhandler",500)

Func NPhandler()
for $n = 0 to UBound($aHwins) - 1
 ;do whatever
 if $aHwins[$n][0] <> 0 and $aHwins[$n][2] <> 0
    ;blah blah
 endif
next

endfunc

Thanks again martin.

I have only started with arrays so may take some time for me to get back to this thread with a reply ;) I understand [10][5] but progressing still with UBound and when to use it and how to formulate it all together.

I will let you know how I go.

thanks again

Share this post


Link to post
Share on other sites

Thanks again martin.

I have only started with arrays so may take some time for me to get back to this thread with a reply ;) I understand [10][5] but progressing still with UBound and when to use it and how to formulate it all together.

I will let you know how I go.

thanks again

Good luck. Understanding arrays can be difficult at first but it's a good investment. You can deal with a lot more in programming if you can use arrays and they can make your scripts smaller, simpler and easier to maintain.

When I started learning to program I used a grid sheet and entered values on the grid to keep track of what I was doing with arrays, and to help my understanding. Once I'd got the hang of it I was able to write a program to replace someone who spent 7 hours a day performing a complex proceedure. He had a much better life after that, but it would not have been possible without using arrays.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

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