Jump to content

multi threading


 Share

Recommended Posts

@TheDcoder

" As we know, AU3 is singlethread and multithreading is only possible through direct ASM. This UDF provides a multithreading emulation through running multiple instances of your script. It also provides an easy way for sending and receiving messages between threads (temporary files are used)."

So I do not know why he does not call "two processes" or "two threads" which is "multi thread" here.
Examples just define how AuThread running with "two threads" look like "CoProc.au3" or "ChildProc.au3", we can edit for "three threads" or more.

If its really "multi threads emulation" try to send and get messages via memory with "_SharedVar.au3" and create thread with THIS

=> That what i meant to say

 

I love this idea, will make non-blocking simulator in AutoIt.

Link to comment
Share on other sites

  • Replies 60
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

@WormIT Don't get your hopes high yet, AutoIt is not multi-thread safe... meaning that you can expect "unexpected" crashes :blink:

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

@Jefrey my editor code for AuThread.au3 newest update version.

Quote

Running on [AutoIt 3.3.14.2] [AutoIt3Wrapper v.15.920.938.0] [SciTE v.3.6.0.0] [OS:WIN_7/Service Pack 1]  [CPU:X64 OS:X64]

In "AuThread.au3"
+ Rename "thread.au3" back to "AuThread.au3" cuz i like this name
+ Need Enval and using MustDeclareVars to avoid running bugs, i don't know why.
+ DllCallbackFree($aResult) , not DllCallbackFree($hHandle). I don't know why, again.
+ Sleep(50) for slowly dllcall, as fast as crash :drool:
+ I also catch params error here

 

In "Example.au3"
+ I'm trying to test with 100 test case very fast. 200 sometimes crashes
+ All GlobalVar again

Happy Scripting! :shifty:

Example.au3

#include 'AuThread.au3'
#include 'Array.au3'

Opt("MustDeclareVars", 1)

Global $h0, $h1, $h2, $h3
Global $testNumber = 100

Dim $testCase[101][8]

ProgressOn("Creating an arrays of test case", "")

For $i = 0 To $testNumber
    ProgressSet($i)

    $h0 = CreateThread("whatever")
    $testCase[$i][0] = $h0

    $h1 = CreateThread("iamsub")
    $testCase[$i][1] = $h1

    ; Opens another one
    $h2 = CreateThread("iamanothersub")
    $testCase[$i][2] = $h2

    ; And opens another one, that will just close itself (as an example)
    $h3 = CreateThread("openandclose")
    $testCase[$i][3] = $h3

    $testCase[$i][4] = "I am the main thread"

    $testCase[$i][5] = TerminateThread($h1) ; Terminates h1 thread
    $testCase[$i][6] = TerminateThread($h2) ; Terminates h2 thread
    $testCase[$i][7] = TerminateThread("") ; Terminates no threads
Next

ProgressOff()

_ArrayDisplay($testCase, "AuThread.au3 " & $testNumber & "test case", "", 0, "|", "whever|sub1|sub2|open&close|main|termiTh1|termiTh1|termiNO")

Func iamsub($hThread)
    ConsoleWrite($i & "|")
EndFunc   ;==>_iamsub

Func iamanothersub($hThread)
    ConsoleWrite($i & @CRLF)
EndFunc   ;==>_iamanothersub

Func openandclose($hThread)
    Return TerminateThread($hThread)
    ConsoleWrite("This message will not show up" & @CRLF)
EndFunc   ;==>openandclose

 

AuThread.au3

#include-once
#include 'Array.au3'
#cs
    AutoIt Threads
    http://github.com/jesobreira/thread.au3
#ce
Global $hHandle, $tStruct, $aResult, $dReturn, $aTerminate, $hThread

Func CreateThread($sCallback)
;~  Local $hHandle, $tStruct, $aResult, $dReturn
    $hHandle = DllCallbackRegister($sCallback, "int", "ptr")
    If Not $hHandle Then Return False
    $tStruct = DllStructCreate("Char[200];int")
    DllStructSetData($tStruct, 1, 10)
    $aResult = DllCall("kernel32.dll", "hwnd", "CreateThread", "ptr", 0, "dword", 0, "long", DllCallbackGetPtr($hHandle), "ptr", DllStructGetPtr($tStruct), "long", 0, "int*", 0)
    $dReturn = (Not @error And IsArray($aResult)) ? $aResult[0] : SetError(1, 0, False)
    DllCallbackFree($aResult)
    Sleep(50)
    Return $dReturn
EndFunc   ;==>CreateThread

Func TerminateThread($hThread, $dwExitCode = 0)
    If Not IsHWnd($hThread) Then Return False
    $aTerminate = DllCall("kernel32.dll", "bool", "TerminateThread", "handle", $hThread, "dword", $dwExitCode)
    $dReturn = (Not @error And IsArray($aTerminate)) ? $aTerminate[1] : SetError(1, 0, False)
    Sleep(50)
    Return $dReturn
EndFunc   ;==>TerminateThread

 

Edited by WormIT
Link to comment
Share on other sites

Maybe you have an example with

a. sorting 100.000 elements in an array

b. GDI+ circles bouncing independent over the screen

Regarding memory maybe you can use memory mapped files createfilemapping and openfilemapping in 2nd process or 2nd thread

on my system it breaks (run from scite and as compiled exe on W10 64 bits) 

Edited by junkew
Link to comment
Share on other sites

  • 1 year later...

Hi all,

I know this thread is old, but AuThread is still amazing. :)

I have one question... I want to use two or more threads at the same time.

Works so far, but how can I use "_AuThread_GetMessage()" for the right thread (for the specific PID)?

What exactly does this mean: "_AuThread_GetMessage( [ $iPID = @AutoItPID ] )"

Thanks for your help. :)

 

greetings

borsTiHD

Link to comment
Share on other sites

  • Moderators

@borsTiHD The help file is your friend...

Quote
@AutoItPID    |    Process identifier (PID) of the current script.

 

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

On 6.6.2018 at 2:13 PM, JLogan3o13 said:

@borsTiHD The help file is your friend...

 

Thanks :D I thought it was some AuThread specific variable, not from AutoIt.

But can someone please help me with my first question?

If I let two threads running, I'm waiting in my main thread for there answers.

Currently I can't assign the individual thread answers to the right thread id.

 

€dit:

Ok, it works now.

I send my ThreadID to my thread and put it afterwards in my thread answer for my main thread.

Now I can use many different threads for different tasks and I know exactly which thread answered me. :)

 

Edited by borsTiHD
Link to comment
Share on other sites

  • Moderators

kcalb7,

Welcome to the AutoIt forums. But when you reply in future, please use the "Reply to this topic" button at the top of the thread or the "Reply to this topic" editor at the bottom rather than the "Quote" button - people know what they wrote and it just pads the thread unnecessarily. Thanks in advance for your cooperation.

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

  • 4 months later...

I just read the Forum Rules and didn't find anything on reply to an old post.

So my question is, how can I use this (AuThread) when i have GUI in my script, since the .exe runs two times I get two GUI. Any solution?

Link to comment
Share on other sites

@Arlen I am not sure that I understand, this UDF does not run the script itself again, but it rather uses a hack to have the callback function (the function which is supplied to the _AuThread_StartThread function) run asynchronously along side the main script... expect a lot of errors though.

Edited by TheDcoder

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

6 minutes ago, TheDcoder said:

@Arlen I am not sure that I understand, this UDF does not run the script itself again, but it rather uses a hack to have the callback function (the function which is supplied to the _AuThread_StartThread function) run asynchronously along side the main script... expect a lot of errors though.

Thank you for clarifying that. BTW do you know any reliable way of doing this multi thread?

Link to comment
Share on other sites

@Arlen Nope, there isn't a good way to do multi-threading in AutoIt because it wasn't designed for this.

The closest thing you could do is run multiple instances of your script which can do multiple jobs for you, but you need to handle the communication between the instances yourself... maybe mail slots are a good way but it depends on your use case :)

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

  • 1 year later...

Hi, this is my first post so if it is in the wrong place I apologize, I'm just looking for some answers here.

I've simplified the actual code I'm using into this script:

 

#include <authread.au3>

_AuThread_Startup()

Func _GetAvailableSlots()

    Global $slots = -1

    $thread1 = _AuThread_StartThread("thread1")

    ConsoleWrite($slots & @CRLF)

EndFunc

Func thread1()

    If (True) Then
        ToolTip("Yes", 0, 0)
        $slots = 3
    Else
        ToolTip("No", 0, 0)
    EndIf
EndFunc

_GetAvailableSlots()

For some reason, the variable $slots prints out as -1 instead of 3. Would anyone have any idea why this is happening? Been stuck on it forever now. Thanks.

Link to comment
Share on other sites

This question would be best asked in the specific AuThread thread in Example Script forum.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

3 hours ago, Petrask24 said:

For some reason, the variable $slots prints out as -1 instead of 3. Would anyone have any idea why this is happening? Been stuck on it forever now. Thanks.

edit sry.  o Its because the slots is only global to the script.  When you create threads using authread variables aren't global across threads.  The threads can communicate with each other using messages, that's one way variables could be changed globally. You could also use a file that's shared between threads.  The reason for this is the way that authread creates the threads, as you know autoit is not multi threaded capable on its own.  To create "threads" authread actually creates a new autoit process for each thread.  That being said your thread1() never actually executes inside the process in which your script is ran.

To expand on this separate processes don't share virtual memory unless some kinda communications pipe is set up or memory is reserved by one process in another.  

Edited by markyrocks
Link to comment
Share on other sites

27 minutes ago, markyrocks said:

edit sry.  o Its because the slots is only global to the script.  When you create threads using authread variables aren't global across threads.  The threads can communicate with each other using messages, that's one way variables could be changed globally. You could also use a file that's shared between threads.  The reason for this is the way that authread creates the threads, as you know autoit is not multi threaded capable on its own.  To create "threads" authread actually creates a new autoit process for each thread.  That being said your thread1() never actually executes inside the process in which your script is ran.

Is it possible to have a return value (or message) from a function within the thread?

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