Jump to content

Multi-thread AutoIt


FredAI
 Share

Recommended Posts

Hello.

This example shows how to create ad manage several threads from a unique autoIt script with the help of an external dll.

I read many people saying it's not possible to have more than one thread in AutoIt. It's not true. We can do anything if we have a helper DLL to do the things we can't do within the autoIt script.

The example also shows how we can make the DLL and the AutoIt code comunicate between themselves.

The zip contains the full code (AutoIt and the C++ project). The script and the DLL are placed in the Release folder.

Feel free to modify and use in any project.

The VC++ project was created with Visual C++ Express 2010. It's free but requires an email registration.

I intend to post a tutorial on how to use your own helper DLL to improve performance and do the things you can't do with AutoIt.

I'ts cool having the AutoIt coding facilities and the power of a general language mixed altogether.

Enjoy!

MT AutoIt.zip

Edited by FredAI
Link to comment
Share on other sites

Here is a couple of funcs, if you could demonstrate them working concurrently it would be a great help.

_Func1()
_Func2()


Func _Func1()
    Sleep(10000)
EndFunc   ;==>_Func1

Func _Func2()
    $i = 1
    While $i < 11
        ConsoleWrite("_Func2" & @LF)
        Sleep(1000)
        $i += 1
    WEnd
EndFunc   ;==>_Func1

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Ok, here's a quick explanation of what happens when you run the script:

First we load kernel32.dll and our MT_AutoIt.dll.

Global Const $Kernel32Dll = DllOpen('Kernel32.dll')
Global Const $MT_AutoItDLL = DllOpen('MT_AutoIt.dll')

Then we create a small GUI using native AutoIt code and display it.

Local $MainGUI = GUICreate('Main thread GUI',400,300), $msg
GUISetState()

Then we create three small structures that will hold the text to pass to the new threads' functions, holded by our dll.

Here's where lies the core of the threading method. While these structs are very simple, they coul have anything we wanted. Tons of info we could pass to the new threads, in order to make them perform more complicated tasks than just displaying a message box, of course.

The first parameter of the _AutoItThreadCreate function, $index is intended to tell the dll which thread calback to use, allowing to create four different functions to perform four different tasks.

You will note that all message boxes are displayed simultaneously, an probably the one thea corresponds to the first thread is not the first one to be displayed. This is because when the CreateThread function is called by the DLL, it returns immediately, even before the thread is started.

in fact we only need one index to have the three message boxes in different threads. The message box that displays "This is thread 3 running in the same function as the thread 1" uses the same calback as the one that displays "This is thread 1".

That means we can create as many threads as we want using always the same calback.

I guess I have to make a better example showing the different calbacks perform different tasks.

Link to comment
Share on other sites

Neat.

Can you show us how to do a callback?

Like say, the message box pops up, and then you click a button and it makes a call back to an autoit function and tells you the button you pressed? :)

I made some modifications so I could compile with MinGW, it's working nicely.

Here is a couple of funcs, if you could demonstrate them working concurrently it would be a great help.

I know you saw the DLL source code, come on, you should know what he was talking about.

He's just trying to introduce people to creating dlls that can spawn a thread and do simple operations, not run another instance of the script interpreter, maybe you should have said he needs to point that out?

Edited by DeputyDerp
Link to comment
Share on other sites

I know you saw the DLL source code, come on, you should know what he was talking about.

Well at first glance, it looks like any threaded work must be coded in the dll file

and AutoIt will still just work in one thread procedurally.

I'm hoping to be dead wrong though.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

where is multi threading ? <_<

Change the topic title please.

It's comments like this one that keep me away from the forums.

Did you even take a look at the code?

Didn't you see that the DLL is creating each message box in a different thread by calling CreateThread?

Didn't you see the message boxes being closed each time TerminateThread is called?

Isn't that multi-threading?

Work first. Comment later.

Link to comment
Share on other sites

Just a flippant comment that FredAl.

MultiThreading, has in my time here, been quite the turbulent subject.

People read the scoop headline and hope for multiple threads in their own

AutoIt code.

I was hoping for the same myself, don't let it dishearten you.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Ok, new example updated. Download in the first post, please.

It's useless if you have to write all threads in C, inside of this DLL. In this case it would be better to write entire application in C.

Well, you don't have to write all the threads, each TreadProc function can run as many threads as you want.

And since you can pass data to the function, it can perform several different tasks depending on the data passed.

The new example shows exactly that. The left window shows the upper part of the desktop scroling in its client area, while the right window shows the bottom. But they both run in the same function's body, in different threads.

Anyway, the point is that you can use the dll to help you not only to create threads, but also to perform other tasks you can't with AutoIt code.

For instance how do you access Windows api macros such as MAKEINTRESOURCE from AutoIt? How about the C++ classes defined in the Windows headers?

Wee, now you can acces them all. Just create a new exportable function in the Dll that calls the macro or the class and returns the desired value to your AutoIt code.

Honestly, I thought this example would be received with more enthusiasm.

I use a helper dll in one of my AutoIt aps, to help with the drawing and big loops. I had a speed improvement of 1000%. Most of the functions that took long time to return, now they return ten times faster.

Link to comment
Share on other sites

@FredAI

I'm sorry to tell you that your example stays the same to the following UDF:

If you do a search on the forum you will find several topics with the same attempt to multi-thread, I do not say that you'll have to give up but it has been tried several times and no one came to desired!

Note: This using the same dll as the dll you have not posted the new example!

JS

Edited by JScript

http://forum.autoitbrasil.com/ (AutoIt v3 Brazil!!!)

Somewhere Out ThereJames Ingram

somewh10.png

dropbo10.pngDownload Dropbox - Simplify your life!
Your virtual HD wherever you go, anywhere!

Link to comment
Share on other sites

DWORD WINAPI _ThreadProc1(LPVOID param)

{

PWSTR wstr = (PWSTR)param;

MessageBox(0,wstr,L"Thread message",MB_OK);

return 0;

} // End of _ThreadProc1

DWORD WINAPI _ThreadProc2(LPVOID param)

{

PWSTR wstr = (PWSTR)param;

MessageBox(0,wstr,L"Thread message",MB_OK);

return 0;

} // End of _ThreadProc1

DWORD WINAPI _ThreadProc3(LPVOID param)

{

PThreadProc3Data thpd = (PThreadProc3Data) param;

HWND desktopHwnd = GetDesktopWindow();

HDC desktopDC = GetDC(desktopHwnd);

int desktopwidth = GetDeviceCaps(desktopDC,HORZRES);

HDC hdcpic = GetDC(thpd->hwnd);

for (int i=0;i<desktopwidth;i++)

{

BitBlt(hdcpic, 0, 0, 240,180, desktopDC, i, thpd->top, SRCCOPY);

//picgr.DrawImage(&desktopBmp,i,thpd->top,240,180);

SleepEx(20,true);

if (i == desktopwidth-240-1) i = 0;

}

return 0;

} // End of _ThreadProc1

DWORD WINAPI _ThreadProc4(LPVOID param)

{

PWSTR wstr = (PWSTR)param;

MessageBox(0,wstr,L"Thread message",MB_OK);

return 0;

} // End of _ThreadProc1

the function making autoit source can?

use the new function is edit MT_AutoIt.cpp only?

make autoit source in function can?

I interest am many quite in AutoitScript.From that is [http://cafe.naver.com/autoitscript[/color]] Korea of cafe(blog) to be operating, [size="2"][color="#ff00ff"]English cannot well[/size].Many help it requests.To read, it stands it thanks.

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