A_Index doesn't increase in loop.

Report problems with documented functionality
magusneo
Posts: 45
Joined: 30 Sep 2013, 06:34

A_Index doesn't increase in loop.

27 Feb 2014, 07:29

I have a script which use dllcall to login server.There is a Init method.
In this method first running A_index increases normally,but A_index doesn't increase(alwayse 1) since Init method second running.

Code: Select all

	Init(server,brokerid,accountid,pwd){
......
		this.stat:=0
		while(this.stat<1){
			Sleep,1000
			OutputDebug,% A_Index
			if(A_Index>=5)
				return false
		}
		return true
	}
I could only use a local var to count normally in loop like this.

Code: Select all

this.stat:=0
		myvar:=0
		while(this.stat<1){
			Sleep,1000
			myvar++
			OutputDebug,% myvar
			if(myvar>=5)
				return false
		}
return true




whole script:

Code: Select all

oMD:=new MDclass()

oMD.Init("tcp://8.8.8.8:51213","1000","00000001","123456")
oMD.Init("tcp://8.8.8.8:51213","1000","00000001","123456")
oMD.Init("tcp://8.8.8.8:51213","1000","00000001","123456")
Sleep,1000000
return

class MDclass{
	static stat:=0
	static hModule,pMd,pMsgQueue
	,CTP_CreateMsgQueue,CTP_ReleaseMsgQueue,CTP_StartMsgQueue,CTP_StopMsgQueue,CTP_ClearMsgQueue,CTP_RegOnConnect
	,MD_CreateMdApi,MD_ReleaseMdApi,MD_Connect,MD_Disconnect,MD_RegMsgQueue2MdApi
	__New(){
		this.hModule := DllCall("LoadLibrary", "Str", "C2CTP.dll", "Ptr")
		this.CTP_CreateMsgQueue :=DllCall("GetProcAddress",Ptr,this.hModule,aStr,"_CTP_CreateMsgQueue@0")
		this.CTP_ReleaseMsgQueue :=DllCall("GetProcAddress",Ptr,this.hModule,aStr,"_CTP_ReleaseMsgQueue@4")
		this.CTP_ClearMsgQueue :=DllCall("GetProcAddress",Ptr,this.hModule,aStr,"_CTP_ClearMsgQueue@4")
		this.CTP_StartMsgQueue :=DllCall("GetProcAddress",Ptr,this.hModule,aStr,"_CTP_StartMsgQueue@4")
		this.CTP_StopMsgQueue :=DllCall("GetProcAddress",Ptr,this.hModule,aStr,"_CTP_StopMsgQueue@4")
		this.CTP_RegOnConnect :=DllCall("GetProcAddress",Ptr,this.hModule,aStr,"_CTP_RegOnConnect@8")
		
		this.MD_CreateMdApi :=DllCall("GetProcAddress",Ptr,this.hModule,aStr,"_MD_CreateMdApi@0")
		this.MD_ReleaseMdApi :=DllCall("GetProcAddress",Ptr,this.hModule,aStr,"_MD_ReleaseMdApi@4")
		this.MD_Connect :=DllCall("GetProcAddress",Ptr,this.hModule,aStr,"_MD_Connect@24")
		this.MD_RegMsgQueue2MdApi :=DllCall("GetProcAddress",Ptr,this.hModule,aStr,"_MD_RegMsgQueue2MdApi@8")
		
		this.pMsgQueue:=DllCall(this.CTP_CreateMsgQueue)
		this.pMd:=DllCall(this.MD_CreateMdApi)
		DllCall(this.CTP_RegOnConnect,Ptr,this.pMsgQueue,Ptr,RegisterCallback("MD_OnConnect", "", "", &this))
	}
	__Delete(){
		DllCall(this.MD_ReleaseMdApi,Ptr,this.pMd)
		DllCall(this.CTP_StopMsgQueue,Ptr,this.pMsgQueue)
		DllCall(this.CTP_ReleaseMsgQueue,Ptr,this.pMsgQueue)
		DllCall("FreeLibrary", "Ptr", this.hModule)
	}
	Init(server,brokerid,accountid,pwd){
		static THOST_TERT_RESTART:=0,THOST_TERT_RESUME:=1,THOST_TERT_QUICK:=2
		DllCall(this.CTP_ClearMsgQueue,Ptr,this.pMsgQueue)
		DllCall(this.MD_RegMsgQueue2MdApi,Ptr,this.pMd,Ptr,this.pMsgQueue)
		DllCall(this.MD_Connect,Ptr,this.pMd,Astr,"account\\" . accountid . "\\",Astr,server,Astr,brokerid,Astr,accountid,Astr,pwd,Int,THOST_TERT_QUICK,Astr,"",Astr,"")
		DllCall(this.CTP_StartMsgQueue,Ptr,this.pMsgQueue)
		this.stat:=0
		while(this.stat<1){
			Sleep,1000
			OutputDebug,% A_Index  ;it doesn't increase since second run
			if(A_Index>=5)
				return false
		}
		return true
	}
}

MD_OnConnect(pApi,pRspUserLogin,step){
	Critical,60
	OutputDebug,% "connecting " step
	if(step>=8){
		Object(A_EventInfo).stat:=1
	}
}
Attachments
Archive.7z
dll file needed to run script
(419.5 KiB) Downloaded 124 times
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: A_Index doesn't increase in loop.

27 Feb 2014, 16:59

It seems quite impossible for this to happen under normal circumstances, since the means by which the loop iterates ensures A_Index (g.mLoopIteration) is incremented:

Code: Select all

for (;; ++g.mLoopIteration)
Your callback is being called on a thread other than the script's main thread. This causes difficult-to-predict behaviour since AutoHotkey is not designed to be multi-threaded. It's almost surprising that the script works at all.
magusneo
Posts: 45
Joined: 30 Sep 2013, 06:34

Re: A_Index doesn't increase in loop.

02 Mar 2014, 00:03

Thanks.It's threads issues indeed.I've got it work,stop the thread created by dll-file first before Initing.
I've use this script for a long time,it can login server once trying in most time.Just found this issue because of bad network recently.
I once thought callback function is always being called by a thread other than the script's main thread.It seems registercallback is not be designed for it?
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: A_Index doesn't increase in loop.

02 Mar 2014, 03:48

RegisterCallback just creates a function that the dll can call. *AutoHotkey* is not designed for it, as I said.

Return to “Bug Reports”

Who is online

Users browsing this forum: No registered users and 94 guests