Jump to content

Multithreading Again


xroot
 Share

Recommended Posts

Someone posted this web site about multithreading.

http://www.devarticles.com/c/a/Cplusplus/Multithreading-in-C/

It said,

"a multithreaded program executes portions of itself concurrently."

"All multithreaded programs include an element of parallelism."

"All processes have at least one thread of execution, which is called the main thread."

"The main thread is created when your program begins."

"In a multithreaded program, the main thread creates one or more child threads."

"Each multithreaded process starts with one thread of execution and then creates one or more additional threads."

"In a properly designed program, each thread represents a single logical unit of activity."

"A major issue in multithreaded programs is managing the interaction of the threads."

With all this in mind I created this little script to see if multithreading is possible,

emphasize on managing the threads.

My example is simple which makes me think multithreading will work fine in au3.

After you click ok on the first msgbox, move each msgbox so you can see all four threads.

multithread.zip

Link to comment
Share on other sites

@xroot

This looks interesting.

BUT I doubt that this is the real multithreading.

When you add this at the end of your code. You can indead see that it generated 4 threads.

; --------------------------------------- Thread info --------------------------------------------------------

$strComputer = "."

$objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")

$colProcessList = $objWMIService.ExecQuery ("SELECT * FROM Win32_Process Where Name = 'AutoIt3.exe'")

For $objProcess in $colProcessList
 ConsoleWrite("Process: " & $objProcess.Name & @LF) 
 ConsoleWrite("Process ID: " & $objProcess.ProcessID & @LF) 
 ConsoleWrite( "Thread Count: " & $objProcess.ThreadCount& @LF) 
 ConsoleWrite( @LF)
Next

But when I add this code the ANY other AU3 script.

I get much higher number of threads to see ?

Give it a try.

Regards

ptrex

Link to comment
Share on other sites

this is nice, you just have to figure out how to call one thread many times with different parameters.. at the same time and how to recieve the return values....

Edited by JRSmile
$a=StringSplit("547275737420796F757220546563686E6F6C75737421","")
For $b=1 To UBound($a)+(-1*-1*-1)step(2^4/8);&$b+=1*2/40*µ&Asc(4)
Assign("c",Eval("c")&Chr(Dec($a[$b]&$a[$b+1])));''Chr("a")&"HI"
Next ;time_U&r34d,ths,U-may=get$the&c.l.u.e;b3st-regards,JRSmile;
MsgBox(0x000000,"",Eval("c"));PiEs:d0nt+*b3.s4d.4ft3r.1st-try:-)
Link to comment
Share on other sites

it's quite a stupid thing but I put it in a function to use it whenever you want

_NewThread("_hello")
_NewThread("_goodbye")
MsgBox(0,"","Main thread") ;you have to do something in the main thread or children threads will be closed and nothing will happen
Func _NewThread($sFunctionName)
    Local $TP,$T,$hT=DllStructCreate("hwnd[1]"),$phT=DllStructGetPtr($hT)
    
    $TP=DLLCallbackRegister($sFunctionName,"int","int")
    $T=DllStructCreate("int")
    $Thread=DllCall("Kernel32.dll","hwnd","CreateThread","ptr",0, _
                                                         "int",0, _
                                                         "ptr",DllCallbackGetPtr($TP), _
                                                         "int",0, _
                                                         "int",0, _
                                                         "ptr",DllStructGetPtr($T))
    DllStructSetData($hT,1,$Thread[0],1)
    DllCall("Kernel32.dll","int","CloseHandle","hwnd",DllStructGetData($hT,1,1))
EndFunc

Func _hello($handle)
    MsgBox(0,"","hello")
EndFunc

Func _goodbye($handle)
    MsgBox(0,"","goodbye")
EndFunc

Some Projects:[list][*]ZIP UDF using no external files[*]iPod Music Transfer [*]iTunes UDF - fully integrate iTunes with au3[*]iTunes info (taskbar player hover)[*]Instant Run - run scripts without saving them before :)[*]Get Tube - YouTube Downloader[*]Lyric Finder 2 - Find Lyrics to any of your song[*]DeskBox - A Desktop Extension Tool[/list]indifference will ruin the world, but in the end... WHO CARES :P---------------http://torels.altervista.org

Link to comment
Share on other sites

As far as i know, autoit is not "thread-safe". It meens we'll have no working multithread au3. msgbox() might be the only thread-safe func. I've just tested example and realised, that even consolewrite is not thread-safe: i got system errors in random...

Link to comment
Share on other sites

I wouldn't say that msgbox is safe either

_NewThread("_hello")
_NewThread("_goodbye")
MsgBox(0,"","Main thread");you have to do something in the main thread or children threads will be closed and nothing will happen
Func _NewThread($sFunctionName)
    Local $TP,$T,$hT=DllStructCreate("hwnd[1]"),$phT=DllStructGetPtr($hT)
   
    $TP=DLLCallbackRegister($sFunctionName,"int","int")
    $T=DllStructCreate("int")
    $Thread=DllCall("Kernel32.dll","hwnd","CreateThread","ptr",0, _
                                                         "int",0, _
                                                         "ptr",DllCallbackGetPtr($TP), _
                                                         "int",0, _
                                                         "int",0, _
                                                         "ptr",DllStructGetPtr($T))
    DllStructSetData($hT,1,$Thread[0],1)
    DllCall("Kernel32.dll","int","CloseHandle","hwnd",DllStructGetData($hT,1,1))
EndFunc

Func _hello($handle)
    MsgBox(0,"","hello",7)
EndFunc

Func _goodbye($handle)
    MsgBox(0,"","goodbye",4)
EndFunc

Giggity

Link to comment
Share on other sites

This very good but it DOES NOT take full advantage of multithread, the only way to do this add a source code to the autoit it self.

And autoit should have a value communication build in.

But ya its a very good ex.

but too many error

Link to comment
Share on other sites

Nah, doesn't work.

CODE
_NewThread("_hello")

_NewThread("_goodbye")

While 1

WEnd

Func _hello($handle)

$gui = GUICreate("win 1",400,300,0)

$Button = GUICtrlCreateButton("Hover",50,50,80,23)

GUISetState(@SW_SHOW)

$iOver = 0

While 1

$array = GUIGetCursorInfo($gui)

if $array[4] = $Button Then

if not $iOver Then

GUICtrlSetData($Button,"Hovering")

$iOver = 1

EndIf

Else

If $iOver Then

GUICtrlSetData($Button,"Hover")

$iOver = 0

EndIf

EndIf

WEnd

EndFunc

Func _goodbye($handle)

$gui = GUICreate("win 1",400,300,0)

$Button = GUICtrlCreateButton("Hover",50,50,80,23)

GUISetState(@SW_SHOW)

$iOver = 0

While 1

$array = GUIGetCursorInfo($gui)

if $array[4] = $Button Then

if not $iOver Then

GUICtrlSetData($Button,"Hovering")

$iOver = 1

EndIf

Else

If $iOver Then

GUICtrlSetData($Button,"Hover")

$iOver = 0

EndIf

EndIf

WEnd

EndFunc

Func _NewThread($sFunctionName)

Local $TP,$T,$hT=DllStructCreate("hwnd[1]"),$phT=DllStructGetPtr($hT)

$TP=DLLCallbackRegister($sFunctionName,"int","int")

$T=DllStructCreate("int")

$Thread=DllCall("Kernel32.dll","hwnd","CreateThread","ptr",0, _

"int",0, _

"ptr",DllCallbackGetPtr($TP), _

"int",0, _

"int",0, _

"ptr",DllStructGetPtr($T))

DllStructSetData($hT,1,$Thread[0],1)

DllCall("Kernel32.dll","int","CloseHandle","hwnd",DllStructGetData($hT,1,1))

EndFunc

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