Using multiple threads, getting "CONTINUABLE EXCEPTION ACCESS VIOLATION"

Ask for help, how to use AHK_H, etc.
lamarcgasolridge
Posts: 15
Joined: 25 Apr 2017, 16:06

Using multiple threads, getting "CONTINUABLE EXCEPTION ACCESS VIOLATION"

Post by lamarcgasolridge » 04 Jun 2017, 03:27

I am trying to add another thread to my script.

Code: Select all

script script script script script
script script script script script
script script script script script

AhkThread("
(
Alias(variable1," getvar(variable1) "), Alias(variable2," getvar(variable2) ")
#include function.ahk
function()
exitapp
return
)")

script script script script script
script script script script script
script script script script script
However I keep getting this popup:

Image

Not sure what the deal is. Happens about 20% of the time.

Also, is there any way to pass a variable from the main script to the ahkthread script?

HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: Using multiple threads, getting "CONTINUABLE EXCEPTION ACCESS VIOLATION"

Post by HotKeyIt » 04 Jun 2017, 05:20

This is most likely because you are accessing/changing the variable from multiple threads, I would encourage you to use CriticalObject

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
Otherwise you will need to use CriticalSection to make sure that only one thread has access to the variable.

lamarcgasolridge
Posts: 15
Joined: 25 Apr 2017, 16:06

Re: Using multiple threads, getting "CONTINUABLE EXCEPTION ACCESS VIOLATION"

Post by lamarcgasolridge » 04 Jun 2017, 13:05

I have been trying, but it feels like I am nowhere close to making it work

I am mostly confused at where to put the function and also the variables. I've tried to make it work from this reply you made before to me:

Code: Select all

obj:=CriticalObject()
AhkThread("
(
obj:=CriticalObject(" (&obj) ")
F2::
send, {F1}
msgbox `% obj.variable
Return
)")
F1::
obj.variable:="rainbows" A_Now
return
Esc::ExitApp
All my tries were various forms of the following. Reading the criticalobject/section help pages are outside my league so all I can do is switch things around and pray. ;)

script
variable:=CriticalObject()
AhkThread("
(
variable:=CriticalObject(" (&variable) ")
function()
return
)")
script
msgbox, %variable%
script

- or -

script
obj:=CriticalObject()
AhkThread("
(
obj:=CriticalObject(" (&obj) ")
Alias(variable," getvar(variable) ")
function()
return
)")
script
msgbox, %variable%
script

HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: Using multiple threads, getting "CONTINUABLE EXCEPTION ACCESS VIOLATION"

Post by HotKeyIt » 04 Jun 2017, 13:45

I suggest to stick with CriticalObject and don't use variable and Alias.
Can you show a complete example that is not working for you?

arcticir
Posts: 693
Joined: 17 Nov 2013, 11:32

Re: Using multiple threads, getting "CONTINUABLE EXCEPTION ACCESS VIOLATION"

Post by arcticir » 05 Jun 2017, 04:35

This is an example of what I wrote to H version novice:

Code: Select all

f:=ahkFast("#Persistent`na:=[1,2,3]`ntest(s){`nreturn s`n}")
Sleep, 200
f.set("test","2")
MsgBox %  f.a.1 " " f.test " " f.test([1,2,"c"]).3



ahkFast(this:="",code:="",wait:=""){
	static dll
	if isobject(this)
	{
		s:="__default_Created(){`nstatic _:=__default_Created(),Thread`n"
			. "Thread:=CriticalObject(" (&this) "),Thread.base:={__Call:CriticalObject(Func(`"__default_call`")),__set:CriticalObject(Func(`"__default_set`")),__get:CriticalObject(Func(`"__default_get`"))}`n"
			. "}`n__default_get(f,n,p:=`"`"){`nglobal`nTry`nreturn (p?`%n`%[p]:`%n`%)`n}`n"
			. "__default_set(f,n,p){`nglobal`nTry`nreturn (`%n`%:=p)`n}`n"
			. "__default_call(f,n,p*){`n`nif n:=func(n)`nreturn p.Length()?n.call(p*):n.call()`n}`n" code
		n:=this.new,n[s]
	}
	else
	{
		if !dll
			UnZipRawMemory(LockResource(LoadResource(0,ad:=FindResource(0,"F903E44B8A904483A1732BA84EA6191F",10))),SizeofResource(0,ad),dll)
		f:=CriticalObject(),f[""]:=r:=MemoryLoadLibrary(&dll),f.fast:=func("ahkFast")
		for i,n in {"fn":["ahkFunction","s==sssssssssss"],"fp":["ahkPostFunction","i==sssssssssss"],"new":["ahktextdll","ut==sss"]
			,"add":["addScript","ut==si"],"exec":["ahkExec","ut==sui"],"get":["ahkgetvar","s==sui"],"set":["ahkassign","ui==ss"],"sub":["ahkLabel","ui==sui"]
			,"pause":["ahkPause","i==s"],"end":["ahkTerminate","i==i"],"Ready":["ahkReady",""],"Reload":["ahkReload","i==i"]}
			f[i]:=DynaCall(MemoryGetProcAddress(r,n.1),n.2)
		if this!=""
			ahkFast(f,this,code)
		return f
	}
}

Galaxis
Posts: 73
Joined: 04 Feb 2016, 20:09

Re: Using multiple threads, getting "CONTINUABLE EXCEPTION ACCESS VIOLATION"

Post by Galaxis » 28 Oct 2020, 10:59

Greetings!
I hope this helps. :wave:
lamarcgasolridge wrote:
04 Jun 2017, 03:27
Also, is there any way to pass a variable from the main script to the ahkthread script?

Code: Select all


;pass Variable From MAIN SCRIPT to THREAD


obj:=CriticalObject()
obj.1:="Fire"
obj.2:="Wind"
obj.3:="Ice"

AhkThread("
(

obj:=CriticalObject(" (&obj) ")

msgbox % obj.1 ""  "" obj.2 ""  "" obj.3


)")


;-----------------------------REVERSED------------------------------------------/
;             Pass Variable From THREAD to MAINSCRIPT


Count:=CriticalObject()

AhkThread("
(

	object:=CriticalObject(" (&Count) ")
	
	object.1:=""One""
	object.2:=""Two""
	object.3:=""Three""


)")


msgbox % Count.1 " " Count.2 " "  "" Count.3



F1::exitapp 
return

Post Reply

Return to “Ask for Help”