- lpCriticalSection:=CriticalSection() ;create and Initialize a new CriticalSection and return pointer which can be used in Lock() and UnLock().
- Lock(lpCriticalSection) ;EnterCriticalSection - other threads will be waiting for your code to finish
- UnLock(lpCriticalSection) ;DeleteCriticalSection - now other threads can access the variable(s).
- CriticalSection(lpCriticalSection) ;deletes CriticalSection
- CriticalSection() ;deletes all CriticalSections
Example should explain everything else.
You will need AutoHotkey.dll and AhkDllThread as well as CreateScript
Here you will find more information: MSDN.
Example requires AutoHotkey_H from this package
SetBatchLines,-1 ;Max performance Loop 2 ;Create 2 CriticalSections and return pointer to use with Lock() and UnLock() _C%A_Index%:=CriticalSection() ;Get pointer to Variables to use in Alias() pX:=getVar(x),pY:=getVar(y) ThreadScript= ( #Persistent Alias(x,%pX% + 0),Alias(y,%pY% + 0) ;create Alias variables x+y, so you can use same variables in exe and dll Loop 2 SetTimer,Label`%A_Index`%,50 ;Label1 and Label2 will be launched rapidly Return Label1: Lock(%_C1%) x.="new" UnLock(%_C1%) Return Label2: Lock(%_C2%) y.="newtext" UnLock(%_C2%) ) dll:=AhkDllThread("AutoHotkey.dll") dll.ahktextdll(ThreadScript) Loop { Loop 2 Lock(_C%A_Index%) lengthX:=StrLen(x),lengthY:=StrLen(y) Loop 2 UnLock(_C%A_Index%) ToolTip % lengthX "-" lengthY } Esc::ExitAppTo run it with AutoHotkey_L you will need LowLevel:
SetBatchLines,-1 ;Max performance [color=red]LowLevel_Init()[/color] ;Create CriticalSection and return pointer to use with Lock() and UnLock() Loop 2 _C%A_Index%:=CriticalSection() ;Get pointer to Variables to use in Alias() pX:=[color=red]__getVar[/color](x),pY:=[color=red]__getVar[/color](y) ThreadScript= ( #Persistent Alias(x,%pX% + 0),Alias(y,%pY% + 0) ;create Alias variables x+y, so you can use same variables in exe and dll Loop 2 SetTimer,Label`%A_Index`%,50 ;Label1 and Label2 will be launched rapidly Return Label1: Lock(%_C1%) x.="new" UnLock(%_C1%) Return Label2: Lock(%_C2%) y.="newtext" UnLock(%_C2%) ) dll:=AhkDllThread("AutoHotkey.dll") dll.ahktextdll(ThreadScript) Loop { Loop 2 Lock(_C%A_Index%) lengthX:=StrLen(x),lengthY:=StrLen(y) Loop 2 UnLock(_C%A_Index%) ToolTip % lengthX "-" lengthY } Esc::ExitAppCriticalSection.ahk Note :!: AutoHotkey_H and AHK.dll have a build in function Lock() + UnLock(), they are faster than user defined functions
Therefore if you are using AHK_H, delete the Lock and UnLock functions from CriticalSection.ahk.
[color=red]Lock(LPCRITICAL_SECTION){ DllCall("EnterCriticalSection","UInt",LPCRITICAL_SECTION) } UnLock(LPCRITICAL_SECTION){ DllCall("LeaveCriticalSection","UInt",LPCRITICAL_SECTION) }[/color] CriticalSection(LPCRITICAL_SECTION="ÿÿÿÿ"){ static If (LPCRITICAL_SECTION!="" and LPCRITICAL_SECTION!="ÿÿÿÿ"){ DllCall("DeleteCriticalSection","Uint",LPCRITICAL_SECTION) Return } else if (LPCRITICAL_SECTION=""){ Loop % (count){ DllCall("DeleteCriticalSection","Uint",&CriticalSection%count%) count:=0 } Return } count++ VarSetCapacity(CriticalSection%count%,24) DllCall("InitializeCriticalSection","Uint",&CriticalSection%count%) Return &CriticalSection%count% }