LoadFile - Load script file as a separate process

Post your working scripts, libraries and tools for AHK v1.1 and older
lexikos
Posts: 9560
Joined: 30 Sep 2013, 04:07
Contact:

Re: LoadFile - Load script file as a separate process

21 May 2021, 05:07

jsong55 wrote:
21 May 2021, 02:26
Does this sort of create "multi threading?"
What does it mean to "sort of create" multi threading? :facepalm:

It starts a separate AutoHotkey process, with which it communicates via a registered COM object. It is essentially the same as how Internet Explorer and Microsoft Word can be automated.

The two processes are fundamentally just separate scripts, but if one calls into the other via a COM object, it cannot do anything while waiting for the call to return.
jsong55
Posts: 228
Joined: 30 Mar 2021, 22:02

Re: LoadFile - Load script file as a separate process

21 May 2021, 06:01

lexikos wrote:
21 May 2021, 05:07
jsong55 wrote:
21 May 2021, 02:26
Does this sort of create "multi threading?"
What does it mean to "sort of create" multi threading? :facepalm:

It starts a separate AutoHotkey process, with which it communicates via a registered COM object. It is essentially the same as how Internet Explorer and Microsoft Word can be automated.

The two processes are fundamentally just separate scripts, but if one calls into the other via a COM object, it cannot do anything while waiting for the call to return.
Ah ic, I have not really tried it. Not sure when is a good occasion to use it. But I get you now I think, while waiting to evaluate the object, it will not go to the next line of code. Hence not multiple threads.
lexikos
Posts: 9560
Joined: 30 Sep 2013, 04:07
Contact:

Re: LoadFile - Load script file as a separate process

21 May 2021, 06:27

jsong55 wrote:Hence not multiple threads.
That's one way of looking at it... but wrong. If you run two processes at once, they can obviously both do things at the same time. Separate processes do not all live within one thread; they each have their own thread(s).

LoadFile starts a separate AutoHotkey process, which contains its own separate thread to execute its script. Both AutoHotkey processes can execute code at the same time, independently.

Only when one script calls the other script's COM object, the caller has to wait for the method or property to return, and cannot do anything while it is waiting. After it returns, both scripts can continue doing their own thing at the same time.

Your question is just "sort of" meaningless. There are multiple ways for a program to appear to do two things at the same time, and they don't all involve multiple OS-level threads. Merely having multiple threads won't necessarily allow you to achieve your goal, whatever that is. LoadFile may or may not help.
Tre4shunter
Posts: 139
Joined: 26 Jan 2016, 16:05

Re: LoadFile - Load script file as a separate process

21 May 2021, 12:57

This is how id use it to 'multithread' although i undesrtand its not really multithreading in this case. But it allows to run 'blocking' events and not have the main script seem like its waiting.

Just using a timer to wait and see if the global vars have data assigned to them.

Main script:

Code: Select all

#Persistent
#SingleInstance, Force
SetBatchLines, -1
SetWorkingDir, A_ScriptDir

#Include Loadfile.ahk

settimer, CheckScripts, 500

S1 := LoadFile("ScriptOne.ahk")

i=0
return

CheckScripts:
	if !IsObject(S1.G["OrderData"]) && !IsObject(S1.G["OrderData"])
		tooltip, % "loop" i++
	
	if IsObject(S1.G["ItemData"]) && IsObject(S1.G["OrderData"])
		tooltip, % "Secondary Scripts vars available"
return

ESC:
exitapp
Secondary Script:

Code: Select all

Global OrderData,ItemData

SetTimer, ReadOrders, 10
SetTimer, ReadItems, 10
return

ReadOrders:
	OrderData := ADO_Read("SELECT * FROM Orders","Provider=VFPOLEDB.9;Data Source=C:\Users\Matt\Desktop\Work Projects\HomePrgOrdersFile")
	SetTimer, ReadOrders, Off
return

ReadItems:
	ItemData := ADO_Read("SELECT * FROM Items","Provider=VFPOLEDB.9;Data Source=C:\Users\Matt\Desktop\Work Projects\HomePrgOrdersFile")
	SetTimer, ReadItems, Off
return

Exitapp
Is there a better way to do what im getting at here, aside from using _H?
User avatar
joedf
Posts: 8940
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: LoadFile - Load script file as a separate process

21 May 2021, 14:25

@Tre4shunter Not really commenting on the functionality of the scripts, mostly a side-note... :thumbup:
You can specify a negative time/period for your SetTimer calls if you want them to run just once ...

Code: Select all

SetTimer, ReadOrders, -10
removing the need of ...

Code: Select all

SetTimer, ReadOrders, Off
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
lexikos
Posts: 9560
Joined: 30 Sep 2013, 04:07
Contact:

Re: LoadFile - Load script file as a separate process

21 May 2021, 18:55

@Tre4shunter
What is the purpose of the ReadOrders/ReadItems timers?

If ReadOrders is waiting for an external COM method to return (such as Execute (ADO Connection)), the ReadItems timer won't execute. In between COM method calls, the ADO_Read function could be interrupted by the ReadItems timer, causing the first ADO_Read call to be suspended while the second one executes. I'd guess you don't actually want them to execute at the same time, otherwise it would have made much more sense to create two secondary scripts instead of one. Why wouldn't you just call them in sequence?

Calls such as S1.G["ItemData"] will not be responsive if S1 is waiting for a COM method to return. Rather than having the main script poll for a result, the secondary script should notify the main script when it is ready. Rather than using LoadFile, I would suggest you use ObjRegisterActive directly, or some other method of communication, such as WM_COPYDATA.

LoadFile creates the client object and registers it. The secondary script then retrieves (a COM proxy for) this object, which it also assigns to client. LoadFile.Serve creates an object in the secondary script and passes it back to the main script by assigning it to client._proxy. If client had any methods, the secondary script could call them; or it could assign results directly to properties such as client.ItemData. However, LoadFile only uses it to pass the secondary proxy back, so it doesn't keep a reference to client.

In short, you can create your own registered object with a method which the secondary script should call when both operations are completed.

WM_COPYDATA is simpler and would probably work just as well or better for your purpose. When the secondary script completes its work, it can combine both results and send them with a single WM_COPYDATA message. When the message is received by the main script, a function is called and it retrieves the results from the WM_COPYDATA parameter.
20170201225639
Posts: 144
Joined: 01 Feb 2017, 22:57

Re: LoadFile - Load script file as a separate process

15 Oct 2021, 23:01

This is such a useful script.

I use it to run a child script that scans the markdown files in a folder periodically and refresh a word index. So when I want to search them, I can just read the global index object in the child script. The file scanning occurs in a different process so won't interrupt time-sensitive hotkeys in the main script.

I also need to start a python com server at script start. Putting it in the main script means for 500ms the script is unresponsive. Putting it in the child script solves that completely.
hasantr
Posts: 933
Joined: 05 Apr 2016, 14:18
Location: İstanbul

Re: LoadFile - Load script file as a separate process

03 Jul 2022, 00:31

Is there a solution to do this kind of operation with AHKV2?
lexikos
Posts: 9560
Joined: 30 Sep 2013, 04:07
Contact:

Re: LoadFile - Load script file as a separate process

03 Jul 2022, 00:34

Yes, but only by using the same techniques as in v1...
lexikos
Posts: 9560
Joined: 30 Sep 2013, 04:07
Contact:

Re: LoadFile - Load script file as a separate process

23 Dec 2022, 02:55

I have updated the script to version 1.1.
  • It no longer depends on ObjRegisterActive or CreateGUID.
  • It now utilizes the /include switch instead of writing code to stdin, so requires AutoHotkey v1.1.35+.
    This fixes issues with A_ScriptFullPath, #SingleInstance, etc.
  • The child process initialization now happens before the script's static initializers instead of after.
User avatar
elModo7
Posts: 217
Joined: 01 Sep 2017, 02:38
Location: Spain
Contact:

Re: LoadFile - Load script file as a separate process

28 Dec 2022, 03:46

Working great for offloading some heavy tasks while keeping track of changes, I haven't used it extensively but I can start to see some usages for it, I guess COM access may be faster than connecting them via tcp? that's how I've been doing separate tasks now, I never ended up benchmarking the difference between that and onmessage for sharing data between instances.

Anyone have any advice on this for better practices and to save some time or should I check on my own anyways?
20170201225639
Posts: 144
Joined: 01 Feb 2017, 22:57

Re: LoadFile - Load script file as a separate process

13 Aug 2023, 07:06

This script is working great for me, but as I'm migrating my scripts to v2, I find that my reliance on it has proved an obstacle!

Anyone has a working v2 version?
hitman
Posts: 21
Joined: 10 Aug 2014, 06:47

Re: LoadFile - Load script file as a separate process

21 Oct 2023, 05:19

Anyone has a working v2 version?

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 71 guests