Page 1 of 1

ProcedureReturn of a Thread

Posted: Sun Jan 26, 2014 12:49 pm
by Wolfram
Hi!
Can someone explain me how to get the ProcedureReturn of a Thread?

Thanks

Wolfram

Re: ProcedureReturn of a Thread

Posted: Sun Jan 26, 2014 1:03 pm
by ts-soft
Threads do not have the concept of "return value" because threads run asynchronously.

Re: ProcedureReturn of a Thread

Posted: Sun Jan 26, 2014 1:06 pm
by luis
Maybe you should explain what you are trying to accomplish.

Anyway, procedurereturn return a value to the caller of the procedure.
A procedure started in its own thread through CreateThread() is not "called", so how it could return something ? To what ? To whom ? Think about it.
This suggest you have to use some form of communication between the main process and its threads.
This could be a global variable, a message, or something else.

Threads are one of the most complex subjects, experiment a lot before using them in a real program, and consider carefully if they are really needed for what you are doing.

Re: ProcedureReturn of a Thread

Posted: Sun Jan 26, 2014 1:42 pm
by luis
But to give you a general idea:

Code: Select all

CompilerIf (#PB_Compiler_Thread = 0)
 CompilerError "Please enable THREADSAFE in the compiler options"
CompilerEndIf

Structure ThreadData
 num_a.i
 num_b.i
 result.i
EndStructure

Procedure TProc(*p.ThreadData)
 Debug "[THREAD] starting to work on the problem"
 Delay(1000)
 Debug "[THREAD] almost there..."
 Delay(1000)
 *p\result = *p\num_a + *p\num_b
 Debug "[THREAD] done !"
EndProcedure

Procedure Main()
  Protected param.ThreadData ; communication structure
  Protected TID ; thread id
  
  param\num_a = 10
  param\num_b = 3
  
  ; the thread will calculate the sum of 10 + 3
  
  TID = CreateThread(@TProc(), @param) 
  
  Debug "[MAIN PROCESS] waiting ..."
  WaitThread(TID)
  
  Debug "[MAIN PROCESS] Thread has finished, result is = " + param\result
EndProcedure

Main()
Threadsafe is not really needed in this example, but when unsure better enable it.

Re: ProcedureReturn of a Thread

Posted: Sun Jan 26, 2014 2:02 pm
by Wolfram
I'm running a thread and depended of its result I want to open a MassageRequester, FileRequester or another MassageRequester .

like this...

if threadresult = 2
MessageRequester("Error 1", "xyz")
elseif threadresult = 1
SaveFileRequester("save file", "my file", "*.txt", 0)
else
MessageRequester("Error 2", "abc")
endif

Re: ProcedureReturn of a Thread

Posted: Sun Jan 26, 2014 2:10 pm
by Wolfram
luis wrote:But to give you a general idea:

Code: Select all

CompilerIf (#PB_Compiler_Thread = 0)
 CompilerError "Please enable THREADSAFE in the compiler options"
CompilerEndIf

Structure ThreadData
 num_a.i
 num_b.i
 result.i
EndStructure

Procedure TProc(*p.ThreadData)
 Debug "[THREAD] starting to work on the problem"
 Delay(1000)
 Debug "[THREAD] almost there..."
 Delay(1000)
 *p\result = *p\num_a + *p\num_b
 Debug "[THREAD] done !"
EndProcedure

Procedure Main()
  Protected param.ThreadData ; communication structure
  Protected TID ; thread id
  
  param\num_a = 10
  param\num_b = 3
  
  ; the thread will calculate the sum of 10 + 3
  
  TID = CreateThread(@TProc(), @param) 
  
  Debug "[MAIN PROCESS] waiting ..."
  WaitThread(TID)
  
  Debug "[MAIN PROCESS] Thread has finished, result is = " + param\result
EndProcedure

Main()
Threadsafe is not really needed in this example, but when unsure better enable it.
Is there also a way to send an EventType at the end of the Thread.
So I can ask for the Result in my MainWindow loop instead of WaitThread()?

Re: ProcedureReturn of a Thread

Posted: Sun Jan 26, 2014 3:28 pm
by luis
Yes, just define something like

#My_Event_Thread_Completed = #PB_Event_FirstCustomValue

and use

PostEvent(#My_Event_Thread_Completed)

in the thread.