Multi-Threading Emulation and SetTimer's Topic.

Helpful script writing tricks and HowTo's
User avatar
Kenzis
Posts: 15
Joined: 01 May 2020, 13:05

Multi-Threading Emulation and SetTimer's Topic.

24 Jul 2023, 13:03

This is an interesting topic I'd like to share. I want to explain how you can achieve multi-threading in AutoHotkey, simulating an ASync-like behavior. (Note: I'm a beginner, and this fascinates me).

Let's start with SetTimers, which are less complicated. SetTimers allow us to call specific subroutines or labels at regular intervals. While it's not the same as ASync, using this function allows us to mimic some aspects of asynchronous computation. SetTimer is particularly useful when dealing with messy code or functions that don't need to be executed immediately. We can alter the delay using variables, making it longer (up to a maximum we set) or shorter, even executing SetTimer immediately. It's amazing, but there's a catch.

This isn't true multi-threading, as we are still using one line of code. Although we can time and execute each code segment depending on our set time, it can be extremely precise. However, it still lacks the ability to execute multiple lines without one affecting the other (which is the essence of multi-threading). Despite this, it's great for games and performance improvements. Though AutoHotkey isn't primarily intended for games, I managed to create a game using SetTimers without any other ASync-like techniques, and it works wonderfully.

Now, let's explore Multi-Threading and what it enables us to achieve:

Simple---

1. Better Performance
2. Faster Computation and Greater Precision

Precision comes into play when we time two scripts to act as one function, which might not be possible in a single script due to speed limitations (again, utilizing SetTimers in both script instances).
(The simplest example again that comes to mind, is when you need to render a specific part in a game you made, you can use different instances of scripts to talk to each other, and act as one) (Therefore the term Multi-Threading)

If we were to use only one script to render something, that is pretty big, Maybe something using voxels and rendering a world. It would be really slow, but using more instances can really really help us.

Now how do we do it? Well, the easiest way for me at least would be using a text document or different text documents which all of the instances of scripts can use to talk to each other. We can put everything we need into the text document and therefore each instance can communicate with each other perfectly and precisely. The only thing that is bad is Overhead, which is a term I don't quite understand, But it shouldn't be much of a problem... Probably.

This is just a topic I wanted to share so if anyone's interested in reading, could read it. I think this is quite interesting, since Autohotkey by itself doesn't have a feature like ASync, but using some tricky stuff, we can emulate and mimic the way Multi-Threading works and we can, therefore, achieve ASync-like functions.

The online term of this solution is called inter-process communication or (IPC) for short.
There are definitely more solutions for this, different approaches. But for now this is the easiest one, that anyone can do, using FileRead and FileAppend.

In future then, if you are making a script that is very extensive, you should definitely try this method of multi-threading if your script requires it. Cheers :D

Also if you wish to say and recommend even better solutions for this, Making multi-threading even easier, i'd love to hear it. Thx

Return to “Tutorials (v1)”

Who is online

Users browsing this forum: No registered users and 78 guests