Procedure as a Thread
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by TerryHough.
I have a Procedure that is working perfectly. It is called with four
parameters.
I need to make that to operate as a Thread.
But, when I try that it immediately GPFs. I cannot find the cause.
I have read most of the topics here about threads and still haven't
a clue. I suspect it has something to do with the four string
fields that are used by the Procedure as parameters. I thought
that they would be Global to the Thread. Maybe I am wrong.
Sorry, I can't post the code...
Any suggestions on how to make this work?
Thanks
Terry
I have a Procedure that is working perfectly. It is called with four
parameters.
I need to make that to operate as a Thread.
But, when I try that it immediately GPFs. I cannot find the cause.
I have read most of the topics here about threads and still haven't
a clue. I suspect it has something to do with the four string
fields that are used by the Procedure as parameters. I thought
that they would be Global to the Thread. Maybe I am wrong.
Sorry, I can't post the code...
Any suggestions on how to make this work?
Thanks
Terry
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by PB.
> Any suggestions on how to make this work?
Not without any code!
Here's a simple example showing how to create a looping thread, which
may (or may not) help you out. It'll help newbies, anyway. If you
can post some code, it would definitely help...
> Any suggestions on how to make this work?
Not without any code!
Here's a simple example showing how to create a looping thread, which
may (or may not) help you out. It'll help newbies, anyway. If you
can post some code, it would definitely help...
Code: Select all
Procedure TimedProcedure()
Repeat
Delay(1000) ; Wait 1 second.
MessageBeep_(#MB_ICONASTERISK)
ForEver
EndProcedure
;
CreateThread(@TimedProcedure(),0)
;
OpenWindow(0,200,200,200,150,#PB_Window_SystemMenu,"Chime every second...")
CreateGadgetList(WindowID()) : ButtonGadget(0,20,20,60,20,"Click me")
Repeat
ev=WaitWindowEvent()
If ev=#PB_Event_Gadget
MessageRequester("Wow","Thread is running in the background!",0)
EndIf
Until ev=#PB_Event_CloseWindow
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by TerryHough.
Hmmm... I got past the GPF it appears.
The problem is that variables in the main program needed by the
Thread aren't getting there.
This is my first effort with threads, so I am probably overlooking
the obvious. Silly me, I thought that I should be able to change
a working Procedure into a Thread with minimal effort.
Thanks for the example PB. Got one that shares a variable between
the thread and the main code?
Terry
Hmmm... I got past the GPF it appears.
The problem is that variables in the main program needed by the
Thread aren't getting there.
This is my first effort with threads, so I am probably overlooking
the obvious. Silly me, I thought that I should be able to change
a working Procedure into a Thread with minimal effort.
Thanks for the example PB. Got one that shares a variable between
the thread and the main code?
Terry
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by TerryHough.
Unfortunately, the variables never get to the thread.
Originally posted by Pupil
Can you atleast show how you have declared the procedure you want run as a thread, and show the CreateThread line in your code.
Code: Select all
Procedure GetFileFTP()
If FtpGetFile_(hInternetConnect, FileName$,FTPDnloadDir+"\"+FileName$, 0,
.
.snip.
.
.
EndIf
EndProcedure
.
.snip.
.
CreateThread(@GetFileFTP(),1)
.
.
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Danilo.
Every Thread-procedure MUST have 1 parameter,
see help for CreateThread().
Always ONE param, if you use it or not.
For giving more than 1 parameter, you can use structures.
And Rings is right, if you use strings at the same
time in your program (in the thread and in the main loop,
or in 2 threads at the same time), the program will
all go crazy...
cya,
...Danilo
(registered PureBasic user)
Every Thread-procedure MUST have 1 parameter,
see help for CreateThread().
Always ONE param, if you use it or not.
Code: Select all
Procedure myThread(value) Repeat
Beep_(800,20):Delay(80)
ForEver
EndProcedure
CreateThread(@myThread(),0)
MessageRequester("Info","Thread running",0)Code: Select all
Procedure MyThread(*p.MyStructure)
EndProcedure
CreateThread(@MyThread(),a.MyStructure)time in your program (in the thread and in the main loop,
or in 2 threads at the same time), the program will
all go crazy...
cya,
...Danilo
(registered PureBasic user)
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by TerryHough.
what I need. So, now I have to go learn about structures in PB.
EDITED LATER: Danilo, I haven't been able to get a structure
passed to the thread. I am a newbie to structures, so if possible
could you show the Structure definition also so I can see how it relates to this Thread call? Thanks.
I understand about simultaneous use of the strings. Sure is likely
to mess up things.
Terry
Thanks for the tip on structures Danilo. Sounds like that isOriginally posted by Danilo
For giving more than 1 parameter, you can use structures.
And Rings is right, if you use strings at the same
time in your program (in the thread and in the main loop,
or in 2 threads at the same time), the program will
all go crazy...
what I need. So, now I have to go learn about structures in PB.
EDITED LATER: Danilo, I haven't been able to get a structure
passed to the thread. I am a newbie to structures, so if possible
could you show the Structure definition also so I can see how it relates to this Thread call? Thanks.
I understand about simultaneous use of the strings. Sure is likely
to mess up things.
Terry
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Danilo.
Structure Example for Threads:
Another example with strings:
cya,
...Danilo
(registered PureBasic user)
Structure Example for Threads:
Code: Select all
Structure MyStructure delay.l
freq.l
duration.l
times.l
EndStructure
Procedure myThread(*p.MyStructure)
For a = 0 To *p\times
Beep_(*p\freq,*p\duration):Delay(*p\delay)
Next a
EndProcedure
my.MyStructure
my\freq = 800
my\duration = 100
my\times = 10
my\delay = 100
CreateThread(@myThread(),@my)
MessageRequester("Info","Thread running",0)Another example with strings:
Code: Select all
Structure MyStructure
caption.s
message.s
EndStructure
Procedure myThread(*p.MyStructure)
MessageRequester(*p\caption,*p\message,0)
EndProcedure
my.MyStructure
my\caption = "Information"
my\message = "Inside Thread"
Thread1 = CreateThread(@myThread(),my)
WaitThread(Thread1)
MessageRequester("Info","Thread ended",0)...Danilo
(registered PureBasic user)
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by TerryHough.
That got tme started. Sometimes an example is all you really need.
The way that PB handles describing structures and pointers is very
much different than the language (largely unheard of) that I have
been using for 18 years. Trying to forget enough of that to learn
PB usage is difficult.
You example shows the WaitThread and your example works. At the
moment, my program including the thread does nothing once the Waitthread is executed. Gotta figure out why...
You must have read my mind, because I really need to have the main
program kind of idle while the thread is running until it completes.
I know, it sounds strange, but is necessary to accomplish a few
things I need to do.
Thanks again for your help.
Terry
Thanks Danilo!Originally posted by Danilo
Structure Example for Threads:
That got tme started. Sometimes an example is all you really need.
The way that PB handles describing structures and pointers is very
much different than the language (largely unheard of) that I have
been using for 18 years. Trying to forget enough of that to learn
PB usage is difficult.
You example shows the WaitThread and your example works. At the
moment, my program including the thread does nothing once the Waitthread is executed. Gotta figure out why...
You must have read my mind, because I really need to have the main
program kind of idle while the thread is running until it completes.
I know, it sounds strange, but is necessary to accomplish a few
things I need to do.
Thanks again for your help.
Terry
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by TerryHough.
Danilo,
I want to thank you again for the example thread code using strings.
And, for your continuing imput and help on this forum.
I now have my program operating correctly, and just need a bit more
polish before releasing it to my clients general use.
I could not have done it without your input and assistance.
Terry
Danilo,
I want to thank you again for the example thread code using strings.
And, for your continuing imput and help on this forum.
I now have my program operating correctly, and just need a bit more
polish before releasing it to my clients general use.
I could not have done it without your input and assistance.
Terry
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
- Michael Vogel
- Addict

- Posts: 2820
- Joined: Thu Feb 09, 2006 11:27 pm
- Contact:
Silly question, is the parameter a long type variable?Every Thread-procedure MUST have 1 parameter,<br />see help for CreateThread().<br />Always ONE param, if you use it or not.
[...]
What happens, if I do something like this...
Code: Select all
Procedure xxx(x.w)
...
EndProcedure
Handle=CreateThread(@xxx(),1)
;...or...
x.w=1
Handle=CreateThread(@xxx(),x)You should also be able to pass pointers since on 32 bit windows they are longs. In fact the API CreateThread uses a void* and since this is a command Fred can only wrap it should apply here too.
Visit www.sceneproject.org
