EnterCriticalSection() always throws an error

Ask for help, how to use AHK_H, etc.
Qriist
Posts: 82
Joined: 11 Sep 2016, 04:02

EnterCriticalSection() always throws an error

14 Sep 2022, 12:04

Confounding error here. I was looking around the forums for ways to improve the performance of my first MT script when I came across the suggestion to use a Critical Object for communication between threads. Creating the object works fine but trying to take control of it does not.

EnterCriticalSection() will always provide me with the following error:
Image
(LeaveCriticalSection() incidentally throws the same error - I assume it's related.)

This is all that is required for me to get the error. Adding a long sleep after CriticalObject() or adding keys to obj does not make a difference. I have also tried temporarily disabling my antivirus just incase that operation was getting flagged somehow.

Code: Select all

obj := CriticalObject()	;works
EnterCriticalSection(obj)	;throws error
LeaveCriticalSection(obj)	;also throws its own error
This sample code from @HotKeyIt in another thread works fine and produces the expected incrementing tooltip. Note the lack of EnterCriticalSection().

Code: Select all

obj := CriticalObject() ; Create new critical object
Loop 4 ; Create 4 Threads.
 AhkThread%A_Index% := AhkThread("obj:=CriticalObject(" (&obj) ")`nLoop`nobj[" A_Index "]:= A_Index")
 
Loop ; Show current content of object.
 ToolTip % obj.1 "`n" obj.2 "`n" obj.3 "`n" obj.4
Esc::ExitApp
PC environment:
  • Win10 Pro Build 19045 x64, running as admin
  • freshly downloaded latest version of AHK_H v1 x64
  • ESET Smart Security antivirus
What can I do to fix this error?
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: EnterCriticalSection() always throws an error

14 Sep 2022, 13:20

ure trying to pass a "Critical Object" as is, which is a plain ahk Object, to the winapi EnterCriticalSection, which instead expects to receive a pointer to a CRITICAL_SECTION. u cant pass garbage to to those types of function and expect them not to blow up ur program
image.png
image.png (4.42 KiB) Viewed 1747 times
Qriist
Posts: 82
Joined: 11 Sep 2016, 04:02

Re: EnterCriticalSection() always throws an error

14 Sep 2022, 23:59

omfg you're right i totally misread the docs. Thanks!

Return to “Ask for Help”

Who is online

Users browsing this forum: No registered users and 107 guests