Code Monkey home page Code Monkey logo

true-autoit-multi-threading's Introduction

๐ŸŒˆ True AutoIt Multi-threading

โšก Just an experiment to achieve multi-threading in AutoIt, as a library.

๐Ÿ‘ All issues and pull requests are welcome!

๐Ÿ”” Give a star and subscribe (watch) to get notified about changes.

Usage

Download the latest release (or build from source).

Library files:

  • N.au3 - AutoIt include file
  • N.dll - 32-bit DLL dependency
  • N64.dll - 64-bit DLL (optional if 32-bit only)

Simple example:

#NoTrayIcon
#include 'N.au3'

func task()
  MsgBox(0, '', "Hello, I'm in another thread.")
endfunc

func main()
  local $t = NRun('task')
  NWait($t)
endfunc

NMain('main')

More examples:

See N.au3 to get all APIs.

Build from source

Requirements:

  • Visual Studio 2017+
  • C++ desktop development
  • Windows SDK 8.1+

Build steps:

  • Open true-autoit-multi-threading.sln
  • Restore Nuget packages
  • Choose CPU arch -> x86/x64
  • Press build -> N.dll/N64.dll
  • Copy above DLLs and N.au3 to your script folder

FAQs

How it works?

Please see the C++ source code, I'll update soon ๐Ÿ˜€.

Does it work with compiled EXE.

Yep, it's designed for both running script in AutoIt.exe and compiled (also upx packed) EXE.

Memory leaks after threads done?

After the first thread is created, the memory increases once ~EXE size. And +80kB after each thread done, that isn't memory leaks, they are static members/smart pointers of AutoIt interpreter still alive. They will be freed when process exits.

Is its shared object faster than AutoItObject?

Yesh, it is implemented with hash table. I don't know why AutoItObject lookups property name through array sequentially.

Is there a magic?

Nope.

Thread safe?

Not implemented yet.

true-autoit-multi-threading's People

Contributors

feggaa avatar nomi-san avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

true-autoit-multi-threading's Issues

Spawning thread rapidly may lead to crash

Run script in AutoIt.exe interpreter and spawn thread rapidly may lead to crash.
Because the interpreter lock the script file and next access denied will throw an exception.

for $i = 0 to 10
  NRun('task')
  Sleep(100)  ; just add this
next

In compiled EXE, sleep(10) works fine.

The Script Restart

This library is really cool, I like your idea and I really started including it in my next project

The problem: But I noticed that the script is restarted only once on the first few run

MsgBox appears twice Then the script works great
`
#NoTrayIcon ; Should add this to prevent tray icon in sub-thread.
#include 'N.au3'
; This test spawns a thread on every button click.
$File = 1
func task_1($l)
MsgBox(0, 'Task 1 (ID: ' & NGetId() & ')', "Test")
For $i = 3 to 15
Sleep(1000)
ConsoleWrite(@crlf&' in task_1 -> '& $i)
Next
endfunc

MsgBox(0,Assign("File",$File+1),'hello file : '&$File)

Func main()
local $l = NLocal()
$id = NRun('task_1', $l)
While 1
Sleep(1000)
ConsoleWrite(@crlf&' in Looper')
WEnd
EndFunc

NMain('main')
`

Update :

After the review, I did not notice very well that the Main Function is not restored
Rather, it only implements its output
That is, if we write everything inside the Main it is not re-executed

How to use with _HttpRequest.au3 From HuanHoang

I try to make it work with _HttpRequest and still 2 task same time work, 3 task make error can not open file etc but still running and return random result from only 1,2,3 task (Not all task)

Too many properties bug

func _task($l)
  $l._1 = 0
  $l._2 = 0
  $l._3 = 0
  $l._4 = 0
  $l._5 = 0
  $l._6 = 0
  ...
  $l._13 = 0
  $l._14 = 0  ; <-- crashes
  $l._15 = 0
end

Script stops

Sometimes it works and sometimes it doesn't
Output :

+>Setting Hotkeys...--> Press Ctrl+Alt+Break to Restart or Ctrl+BREAK to Stop.

 main Loop
 main Loop
 main Loop!>14:22:52 AutoIt3.exe ended.rc:-1073741819
+>14:22:52 AutoIt3Wrapper Finished.
>Exit code: 3221225477    Time: 4.037

Script :

#NoTrayIcon
#include 'N.au3'

Func Wait($data)
	$hWin = WinWait("Choose File to Upload")
	For $i = 1 To 5
		If Not WinExists($hWin) Then Exit
        ControlSetText($hWin,"","Edit1",StringReplace("D:/_Qt/MaintenanceTool.exe","/","\"))
		ControlClick($hWin,"","Button1")
		Sleep(100)
    Next
EndFunc


Func main()
	NRun('Wait', NLocal())
	While 1
		ConsoleWrite(@CRLF&' main Loop')
		Sleep(100)
	WEnd
EndFunc

NMain('main')

Limited array datatype supports

#include <Array.au3>
#include './N.au3'

Local $o = NGlobal()
Local $a = [1, 2]
$o.arr = $a

NMain(main)

Func main()

	;Change an element
	;~ $o.arr[0] = 3 ; <-- errorstdout: Statement cannot be just an expression.
	
	Local $t = $o.arr
	$t[0] = 3
	$o.arr = $t

	ConsoleWrite($o.arr[0] & @CRLF)
	


	;Display array
	;~ _ArrayDisplay($o.arr) ; <-- errorstdout: Expected a variable in user function call.

	_ArrayDisplay($t)

EndFunc

Temporary workarounds that I've tried:

  • Display using _ArrayDisplay by assigning the array (from the global state) to a new local variable.
  • Manipulate also by assigning the array to a new local variable, then reassigning it to the global state.

GUIOnEventMode & object lifetime bug

Seems the object is destroyed before thread starts.

Opt('GUIOnEventMode', 1)

func _btn_click()
  local $l = NLocal()
  $l.message = 'Hi, this is a message.'
  NRun('_task', $l)
EndFunc ; <-- $l destroyed

func _task($l)
  local $msg = $l.message ; <-- crashes
  for $i= 1 To 10
    MsgBox(0, '', $msg, 1)
    Sleep(100)
  next
endfunc

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.