CreateVOThread() plays hangman every now and then. My app is po-box between
several servers in several countries.
Most of the time the program is working just fine, but some times(after an
hour, day or week), for no visible reason, the call to CreateVOThread will
start the Thread but doesn't come back into the main program. The thread
itself finishes, but the main program stays frozen(like it's in a dead
while loop).
Who can help me out on this one.
Sample code below
TIA
Rens
Part of main program:
oThr1:=MemAlloc(_SizeOf(_SDThreadInfo))
IF oThr1==NULL_PTR
SELF:ToErrorListbox("Memory allocation failed")
ELSE
SysObject():liMoveThreadsActive+=1
oThr1.hWnd:=SELF:handle()
oThr1.InFile:=StringAlloc(cDirFrom+"\"+aDir[nI,1]+CHR(0))
oThr1.OutFile:=StringAlloc(cDirTo+"\"+aDir[nI,1]+CHR(0))
oThr1.hThread:=CreateVOThread(NULL,0,@OnStartThreadFileMove(),oThr1,0,@dwID)
>> //the program does not return to here, so I can't check the value below
IF oThr1.hThread==NULL
oThr1.dwSucces:=THREAD_CREATION_FAILED
PostMessage(SELF:handle(),SysObject():RegWinMsg,DWORD(_CAST,oThr1),MemFree_F
ileThread)
SysObject():liMoveThreadsActive-=1
END
FUNCTION OnStartThreadFileMove(lpVoid AS PTR) AS DWORD PASCAL
LOCAL sThreadInfo AS _SDThreadInfo
LOCAL OpStruct IS _winSHFileOpStruct
sThreadInfo:=lpVoid
IF IsPtr(sThreadInfo.hWnd)
OpStruct.hWnd:=sThreadInfo.hWnd
OpStruct.wFunc:=FO_MOVE //FO_COPY
OpStruct.pFrom:=sThreadInfo.InFile
OpStruct.pTo:=sThreadInfo.OutFile
OpStruct.fFlags:=_OR(FOF_NOCONFIRMATION,FOF_NOERRORUI)
sThreadInfo.dwSucces:=SHFileOperation(@OpStruct)
END
PostMessage(sThreadInfo.hWnd,SysObject():RegWinMsg,DWORD(_CAST,lpVoid),MemF
ree_FileThread)
SysObject():liMoveThreadsActive-=1
sThreadInfo:=NULL
ExitVOThread(0)
RETURN 0
I´ve never done any multithreading yet, except for playing around with
the web server sample that comes with VO.
However, from what I learnt from Ginny's and Rod's sessions on
multithreading, I would consider a CriticalSection to "protect" at
least the following line:
> SysObject():liMoveThreadsActive-=1
The problem can exist if during this access/assign operation the
thread's time slot is taken away and another thread comes in and tries
to do the same. Remember that what you see as an atomic single line of
code can be in fact several machine instructions, each of one subject
to a potential thread context switch.
Regards
Adriano