Restarting a stopped thread.

Darren66

Newcomer
Joined
Jun 9, 2003
Messages
20
I have created a thread in my code to deal witha lengthy download from the net so the GUI remains reponsive whilst the download takes place. However - I want this thread to run numerous times as the file online changes quite often.
I put the Thread.Start command in a timer and everything works fine the first time the code runs. The second time the code runs I get a ' ThreadStateException' error.

Having consulted the manual I can see that my thread is starting, running and then stopping succesfully BUT that a thread cannot return to the running state once it has stopped.

Can anybody tell me how to get round this problem??

Many Thanks
 
Once the thread has stopped and has been terminated, you will need to create a new thread with the same entry point as the previous one.

Alternatively, you can Suspend the thread (it will stop executing, but not terminate), and then you can resume it again. Generally this is not a good idea, because it can create application deadlocks, if one thread relies on another thread which has been suspended.
 
Last edited:
Cheers Volte
Could I ask your advice on this - I am calling the thread with a Timer event and what may occur is that the timer event fires for the second time before the first thread has finished executing.

Therefore what I wanted was some sort of 'If Thread1.IsAlive Then Exit Sub' line at the beginning of the Timer event to avoid running the rest of the Timer event code until the thread has finished.

If I create a new thread each time the Timer fires I could get some big backlogs build up.
Could you point me in the right direction?
Many Thanks
 
If you are just going to call the thread again once the thread dies, why not just stick a tight loop in your thread so that it keeps looping until you tell it to stop (by way of a boolean variable). Because it's in another thread, a tight loop will not freeze your program.
 
Volte (or anybody)
As per your suggestion I put the code within the thread in a loop and use a boolean to switch it on or off. Basically the thread is alive whilst the Timer is running. This works absolutely fine.

However I noticed that if I started another thread after I had started Thread1 the CPU went upto 100% (from about 15%) and stayed there until the app was closed. I decided to do a few tests looking for contentions without solving it so I deccided to start from the bottom up.
For Thread2 I put a simple msgbox("Hello") command in the new thread and started it (having already started thread1). This also takes the CPU upto 100% and it stays there until I stop the application.
Any ideas?? Am I going mad?
 
Try putting some Sleep in between. All work, no sleep is bad for thread. In the worker thread,
Visual Basic:
While blnKeepRunning
'-Work, work, work...
...
'-Now, sleep a bit.
System.Threading.Thread.Sleep(100)
End While
 
At the end of the routine I have a Thread1.Suspend to which I have now added Thread1.Sleep. I still get the 100% problem unfortunately.
From the timer event I have blnKeepRunning = True and then Thread1.Resume.
I've been looking at this for too long and I'm not thinking straight but do I need the suspend and resume? Otherwise the Do / While loop only run once..



Visual Basic:
While blnKeepRunning
'-Work, work, work...
...
'-Now, sleep a bit.
blnKeepRunning = False
Thread1.Sleep(100)
Thread1.Suspend
End While
 
Do you experience any performance problems? You really shouldn't rely on the CPU usage gauge for looking for problems. Even a loop as simple as this:
Visual Basic:
Do
    Application.DoEvents
Loop
Will send the CPU usage flying.
 
Hi VolteFace,

that program ive been writing has a lot of loops running and the doevents worked fine.. i think Darren's CPU problem would be fixed by a combination of the thread sleeping and a doevents.. try this..


Visual Basic:
While blnKeepRunning
'-Work, work, work...
...
'-Now, sleep a bit.
blnKeepRunning = False
application.doevents
Thread1.Sleep(100)
Thread1.Suspend
End While
 
Back
Top