├── example from the readme.au3 ├── example.au3 ├── authread.au3 └── README.md /example from the readme.au3: -------------------------------------------------------------------------------- 1 | #include 'authread.au3' 2 | 3 | _AuThread_Startup() 4 | 5 | Func myThreadFunction() 6 | While True 7 | ; In this example, we will get a message sent by the main thread (if any) 8 | $sMsg = _AuThread_GetMessage() 9 | If $sMsg Then 10 | ; Message was sent, let's display it 11 | MsgBox(0, "Message from the main thread to the sub thread", $sMsg) 12 | EndIf 13 | 14 | MsgBox(0, "Thread example", "Hey, I am a thread!") 15 | Sleep(1000) 16 | WEnd 17 | EndFunc 18 | 19 | $hThread = _AuThread_StartThread("myThreadFunction") 20 | 21 | While True 22 | MsgBox(0, "", "Hey, I am the main thread!") 23 | _AuThread_SendMessage($hThread, "Hello, sub thread! How are you?") 24 | Sleep(1000) 25 | WEnd -------------------------------------------------------------------------------- /example.au3: -------------------------------------------------------------------------------- 1 | #include 'authread.au3' 2 | 3 | _AuThread_Startup() 4 | 5 | $hThread = _AuThread_StartThread("sendalert") 6 | 7 | Func sendalert() 8 | While 1 9 | $msg = _AuThread_GetMessage() 10 | If $msg Then 11 | MsgBox(0, "Alert from thread", $msg) 12 | EndIf 13 | 14 | ; randomly sends (or not) messages to the main thread 15 | $rand = Random(1, 10, 1) 16 | If $rand = 2 Then 17 | _AuThread_SendMessage(_AuThread_MainThread(), "Hey!") 18 | EndIf 19 | WEnd 20 | EndFunc 21 | 22 | ; main thread 23 | While True 24 | ; randomly sends (or not) messages to the main thread 25 | $rand = Random(1, 3, 1) 26 | TrayTip("Main thread window", "Sorted number: " & $rand, 1) 27 | If $rand = 2 Then 28 | _AuThread_SendMessage($hThread, $rand & " was sorted") 29 | EndIf 30 | 31 | ; get some message sent to the main PID 32 | $msg = _AuThread_GetMessage() 33 | If $msg Then 34 | MsgBox(0, "Wayback message", "The thread sent: " & $msg) 35 | EndIf 36 | Sleep(1000) 37 | WEnd 38 | 39 | ; The line below is just an example, 40 | ; as once the main thread is closed or killed, the UDF will automatically close all the started threads 41 | _AuThread_CloseThread($hThread) -------------------------------------------------------------------------------- /authread.au3: -------------------------------------------------------------------------------- 1 | #cs ---------------------------------------------------------------------------- 2 | 3 | AuThread 4 | written by Jefrey 5 | 6 | #ce ---------------------------------------------------------------------------- 7 | 8 | Global Const $__AuThread_sTmpFile = @TempDir & "\" & @ScriptName & "_thread.tmp" 9 | 10 | Func _AuThread_Startup() 11 | ; was this process called for a thread? 12 | If $CmdLine[0] = 2 And $CmdLine[1] = "--au-thread" Then 13 | AdlibRegister("__AuThread_Checkloop") 14 | Call($CmdLine[2]) 15 | Exit 16 | Else 17 | ; it's the main thread 18 | OnAutoItExitRegister("__AuThread_OnExit") 19 | FileDelete($__AuThread_sTmpFile) 20 | IniWrite($__AuThread_sTmpFile, "main", "pid", @AutoItPID) 21 | EndIf 22 | EndFunc 23 | 24 | Func _AuThread_MainThread() 25 | Local $sRet = IniRead($__AuThread_sTmpFile, "main", "pid", False) 26 | If $sRet = "False" Then $sRet = False 27 | Return $sRet 28 | EndFunc 29 | 30 | Func _AuThread_StartThread($sCallback, $sMsg = Default) 31 | Local $iPID 32 | If @Compiled Then 33 | $iPID = Run(@ScriptFullPath & " --au-thread """ & $sCallback & """") 34 | Else 35 | $iPID = Run(@AutoItExe & ' "' & @ScriptFullPath & '" --au-thread "' & $sCallback & '"') 36 | EndIf 37 | If $sMsg <> Default Then _AuThread_SendMessage($iPID, $sMsg) 38 | Return $iPID 39 | EndFunc 40 | 41 | Func _AuThread_GetMessage() 42 | Local $sMsg = IniRead($__AuThread_sTmpFile, "msg", @AutoItPID, False) 43 | If $sMsg <> "False" Then 44 | IniDelete($__AuThread_sTmpFile, "msg", @AutoItPID) 45 | Return $sMsg 46 | Else 47 | Return False 48 | EndIf 49 | EndFunc 50 | 51 | Func _AuThread_SendMessage($iPid, $sMessage) 52 | Return IniWrite($__AuThread_sTmpFile, "msg", $iPid, $sMessage) 53 | EndFunc 54 | 55 | Func _AuThread_CloseThread($iPid) 56 | Return ProcessClose($iPid) 57 | EndFunc 58 | 59 | ; ============= internal use only ============ 60 | 61 | Func __AuThread_Checkloop() 62 | ; check if main thread was closed (if so, close this one too) 63 | $iMainThreadPID = IniRead($__AuThread_sTmpFile, "main", "pid", False) 64 | If Not ProcessExists($iMainThreadPID) Then 65 | FileDelete($__AuThread_sTmpFile) 66 | Exit 67 | EndIf 68 | EndFunc 69 | 70 | Func __AuThread_OnExit() 71 | FileDelete($__AuThread_sTmpFile) 72 | EndFunc -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | AuThread 2 | ======== 3 | 4 | This UDF provides multithread emulation for AutoIt3. 5 | 6 | AutoIt supports only one thread per script. This UDF emulates multiple threads by opening multiple processes and sending messages through temporary files. 7 | 8 | It offers an easy way to emulate threads and exchange messages between the threads. 9 | 10 | AuThread is maintained by [Jefrey](http://). 11 | 12 | How to use 13 | ========== 14 | 15 | Start by including the lib onto your script. 16 | 17 | ``` 18 | #include 'authread.au3' 19 | ``` 20 | 21 | Now call `_AuThread_Startup()`. This function is responsible for calling the callback functions if the currently running process is a "thread", or saving important data to temporary files if it's the main thread. 22 | 23 | ``` 24 | _AuThread_Startup() 25 | ``` 26 | 27 | Now create your thread function. You can choose any name for it or even create multiple functions (for multiple threads). Note that if you want your thread to last an undetermined time, you must include an infinite loop inside the function. 28 | 29 | ``` 30 | Func myThreadFunction() 31 | While True 32 | ; In this example, we will get a message sent by the main thread (if any) 33 | $sMsg = _AuThread_GetMessage() 34 | If $sMsg Then 35 | ; Message was sent, let's display it 36 | MsgBox(0, "Message from the main thread to the sub thread", $sMsg) 37 | EndIf 38 | 39 | MsgBox(0, "Thread example", "Hey, I am a thread!") 40 | Sleep(1000) 41 | WEnd 42 | EndFunc 43 | ``` 44 | 45 | Now start the thread by calling: 46 | 47 | ``` 48 | $hThread = _AuThread_StartThread("myThreadFunction") 49 | ``` 50 | 51 | This function returns the PID of the process-thread. You'll use it for sending messages to threads. 52 | 53 | You can continue writing your script on the same file: 54 | 55 | ``` 56 | While True 57 | MsgBox(0, "", "Hey, I am the main thread!") 58 | _AuThread_SendMessage($hThread, "Hello, sub thread! How are you?") 59 | Sleep(1000) 60 | WEnd 61 | ``` 62 | 63 | By default, Windows API (and therefore AutoIt) allows only 1 MessageBox per thread. However, by running the script we've just written, you'll see that we're capable of running two message boxes at once on the same script. 64 | 65 | Also, when you close the main script, all the threads are also closed. 66 | 67 | You cannot start a thread directly without opening the main thread. 68 | 69 | Docs 70 | ==== 71 | 72 | void _AuThread_Startup() 73 | ------------------- 74 | Verifies if the currently running script is a thread being called by a mainthread or if it's the main thread itself. If it's being called by a main thread, it calls the specified callback function. Else, it will just save required data for the UDF. 75 | 76 | int _AuThread_MainThread() 77 | -------------------------- 78 | This function can be called in both threads and mainthreads. It will always return the PID of the main thread. 79 | 80 | int _AuThread_StartThread(string $sCallback [, string $sMsg ] ) 81 | --------------------------------------------------------------- 82 | Starts a thread and returns the new thread PID. $sCallback must be a callable function name. If $sMsg is specified, `_AuThread_SendMessage($sMsg)` will be automatically called. 83 | 84 | string _AuThread_GetMessage( [ $iPID = @AutoItPID ] ) 85 | ----------------------------------------------------- 86 | Gets the last message sent to the current thread (or main thread, if called on it), returns and deletes it. If no message is found, False is returned. 87 | 88 | void _AuThread_SendMessage($iPid, $sMessage) 89 | -------------------------------------------- 90 | Sends a message to a thread (main or sub). This message can be retrieved through `_AuThread_GetMessage()`. 91 | 92 | void _AuThread_CloseThread( [ $iPID = @AutoItPID ] ) 93 | ---------------------------------------------------- 94 | Closes a thread or the current thread (if no thread PID is specified). 95 | 96 | 97 | Additional notes 98 | ================ 99 | The thread is the own current script called with the following parameters: 100 | 101 | * `script.exe --au-thread "$sCallback"` (if compiled) 102 | * `@AutoItExe C:\path\to\script.au3 --au-thread "$sCallback"` (if not compiled) 103 | 104 | In order to avoid errors, do not use the `--au-thread` argument (why would you do?). Also, note that no parameters will be sent when calling the threads. Use the UDF messages for that. 105 | 106 | Also, remember that the script will automatically close after calling your callback function. If you don't want so, put a loop (For, While or Do) in your function, like showing above. 107 | 108 | The global constant `$__AuThread_sTmpFile` stores the filename where the thread messages and other informations will be kept. It's based on the file name. You probably won't have to worry about this file. --------------------------------------------------------------------------------