Hi!
Can someone explain me how to get the ProcedureReturn of a Thread?
Thanks
Wolfram
ProcedureReturn of a Thread
ProcedureReturn of a Thread
macOS Catalina 10.15.7
Re: ProcedureReturn of a Thread
Threads do not have the concept of "return value" because threads run asynchronously.
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.

Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.

Re: ProcedureReturn of a Thread
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.
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.
"Have you tried turning it off and on again ?"
Re: ProcedureReturn of a Thread
But to give you a general idea:
Threadsafe is not really needed in this example, but when unsure better enable it.
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()
"Have you tried turning it off and on again ?"
Re: ProcedureReturn of a Thread
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
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
macOS Catalina 10.15.7
Re: ProcedureReturn of a Thread
Is there also a way to send an EventType at the end of the Thread.luis wrote:But to give you a general idea:
Threadsafe is not really needed in this example, but when unsure better enable it.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()
So I can ask for the Result in my MainWindow loop instead of WaitThread()?
macOS Catalina 10.15.7
Re: ProcedureReturn of a Thread
Yes, just define something like
#My_Event_Thread_Completed = #PB_Event_FirstCustomValue
and use
PostEvent(#My_Event_Thread_Completed)
in the thread.
#My_Event_Thread_Completed = #PB_Event_FirstCustomValue
and use
PostEvent(#My_Event_Thread_Completed)
in the thread.
"Have you tried turning it off and on again ?"


