Stop thread from working (Downloading)

kill4

Member
Joined
Mar 1, 2011
Messages
5
Programming Experience
Beginner
Ok guys, I need some guidance on how i would properly stop a thread from downloading, if a timeout supplied by the user has elapsed.
I have an array threads declared in the general area of the main form class. looks like this.

VB.NET:
Expand Collapse Copy
Private objThreads(100) As Thread

I start these threads in a for loop in a sub routine. I also start an array of timers, that will dictate when these threads should stop working, meaning a certian amount of time has passed. The question is how would i go about stopping these threads from downloading any more data safely? Without disposing of these threads completly. Since i will restart these threads again to do more work.. Any ideas guys?

Thanks..
 
That's not the way you should go about it. Creating 100 threads of your own is a very bad idea. You're actually going to slow things down. You should be using the ThreadPool to perform some of the operations simultaneously and queue the others until a thread becomes available. You could use the ThreadPool directly or you could use the DownloadFileAsync method of the WebClient class, which inherently uses the the ThreadPool.
 
Actually the user wont use all the threads, I just added the 100 thing so the user could use 100 threads if he wants, but thats way over bloated, and he wont be using 100 threads more like 5 max. He specifies how many threads to use in a combo box, before he hits the start button. That being said im i still going about it the wrong way?

And is there a way to actually stop the thread from getting any more data back this way? Let me show you the timeout sub that is fired when a timeout is elapsed..

VB.NET:
Expand Collapse Copy
Private Sub TimedOut(ByVal Index As Integer)
        objTimeouts(Index).StopTimer() '-- Stops the timeout timer.
        '-- Resseting other varibles here, ect..
        RestartThread(Index)
    End Sub

There another sub is called were i would restart the thread that timed out. It's called RestartThread() and passes the index of the thread as a argument
Some of the code for the restartthread sub below

VB.NET:
Expand Collapse Copy
Private Sub RestartThread(ByVal I As Integer)
 objThreads(I) = New Thread(AddressOf objRequest(I).GetRequest)
objThreads(I).IsBackground = True
objThreads(I).Start()
End Sub

User would get sometimes a error Thread is currently running or has stopped error, on the .start method of the thread. I'm thinking he is getting this error because the thread was still doing work. I was under the impression using the "New" keyword would cause the thread to stop any previous downloading or other working and start a fresh.

So is this still the wrong way to approach this?

Hope i made my self a little more clear :)

Thanks for the reply..
 
Last edited:
I was under the impression using the "New" keyword would cause the thread to stop any previous downloading or other working and start a fresh.
New keyword is used to create a new object, and = assignment operator is used to assign a value/object to a variable. Neither operation does directly affect any existing object.
 
Back
Top