Can i Kill Thread myself ?

xky

Member
Licensed User
Longtime User
when I start a thread, if it take a long time to run, can I kill it myself?
For Exanple:

Thread1.start(me,"SubTest",array)
Thread1.sleep(5000) 'This may be something take a long time but I don't want wait

When Thread1 sleeping can I kill it ?
If I want the thread run 500ms, whether it ended or not, I want it be terminated, How can I ?
:sign0085:
 

Informatix

Expert
Licensed User
Longtime User
when I start a thread, if it take a long time to run, can I kill it myself?
For Exanple:

Thread1.start(me,"SubTest",array)
Thread1.sleep(5000) 'This may be something take a long time but I don't want wait

When Thread1 sleeping can I kill it ?
If I want the thread run 500ms, whether it ended or not, I want it be terminated, How can I ?
:sign0085:

A thread can never be killed. If you want it to abort after a certain period of time or if you want it to end in a particular case, that must be done from inside the thread, not from another thread.

EDIT: a sleeping thread can be stopped by an interruption. Look at the Interrupt function in the library documentation.
 
Last edited:
Upvote 0

xky

Member
Licensed User
Longtime User
Thank you, I can use Interrupt, I do some complex calculate, and it need about 10sec but sometime the calculate can't have a resault, so it will go on for ever,then after 10 sec I want to kill it because it need too much memory.
For example, I generate numbers randomizely and record each number in memory, if I got number 10, the thread will end, but if in 10sec I don't get 10, the thread will go on until it got number 10... So I want to kill it.
:icon_clap:
 
Last edited:
Upvote 0

Informatix

Expert
Licensed User
Longtime User
Thank you, I can use Interrupt, I do some complex calculate, and it need about 10sec but sometime the calculate can't have a resault, so it will go on for ever,then after 10 sec I want to kill it.
For example, I generate numbers randomizely, if I got number 10, the thread will end, but if in 10sec you don't get 10, the thread will go on until it got number 10... So I want to kill it.
:icon_clap:

The simplest thing to do is to create a timer that you enable when you start your computation thread. In the tick handler, you call Interrupt. In your computation thread, you check regularly IsInterrupted. If it's true, you exit.
 
Upvote 0

alan1968

Active Member
Licensed User
Longtime User
The simplest thing to do is to create a timer that you enable when you start your computation thread. In the tick handler, you call Interrupt. In your computation thread, you check regularly IsInterrupted. If it's true, you exit.

hello,
can you give a small example how to get out with a timer?
What should I put in the condition "if my_thread.isinterrupted then" for exit
thank you!
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Start sub with a loop that uses DoEvents sometimes, giving the system a chance to work with all other processes.

If (i Mod 500) = 0 then DoEvents
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
hello,
can you give a small example how to get out with a timer?
What should I put in the condition "if my_thread.isinterrupted then" for exit
thank you!

If you want to stop a background thread after a certain period of time, you can do it this way:
- you create a timer
- you enable the timer just before starting your thread
- you raise a flag when the timer ends (e.g. StopThread = True in the Tick event handler)
- in your thread, you check regularly the flag value
- if the flag is risen, you exit (e.g. if StopThread = True then Return)
 
Upvote 0

alan1968

Active Member
Licensed User
Longtime User
If you want to stop a background thread after a certain period of time, you can do it this way:
- you create a timer
- you enable the timer just before starting your thread
- you raise a flag when the timer ends (e.g. StopThread = True in the Tick event handler)
- in your thread, you check regularly the flag value
- if the flag is risen, you exit (e.g. if StopThread = True then Return)

thx ! works fine :)
 
Upvote 0
Top