Collection(Of Thread) - Store Thread objects problem

Hello,

I have been struggeling for days with getting my application to work. It basically allows a user to download a file from a website, stop/resume the download and various other options.

My problem is that i create a new thread for each file in the download queue. To keep track of all the threads created i store them in a Collection for later use.

Example:

dim MyThreads as New Collection(Of Thread)

Now let's say MyThreads contains 3 threads. Let's abort the work on thread 1 (0-2) = 3

Dim CurrentThread as Thread = MyThreads(1)

CurrentThread.Abort()

How can i create a reference to CurrentThread so that it can attach it to the Thread object so that i can abort it's work

This is how i add a new thread:

dim Worker as New Thread(new ThreadStart(DownloadFileInBackground)

Worker.Name = "test" & Iterator

Worker.Start()

MyThreads.Add(Worker)

But what happens now, is that the object Worker is added to the MyThreads collection, but when i try to stop the thread by CurrentThread.Abort() it says it's not running (Though it's running in Debug -> Threads and the contents from the file downloading is written to the disk) Could the listview.invoke() method cause the thread to not abort

I've also tried to use a ThreadPool, and a Queue, but they do not support resume/stop, so i basically need a class that can handle all my threads, that can stop/resume a Thread.

Regards,
Peter.



Answer this question

Collection(Of Thread) - Store Thread objects problem

  • ivj

    The ThreadState should be one of the values in theThreadState enum.

    You have two options, one isto use the Abortable thread pool as one of the posters have suggested, or set a bool flag for each thread so that they gracefully abort when you ask them to.

    For example say your thread does some repetitive work

    while(someCondition)

    {

    //Do work.

    }

    To stop such a thread, have a flag that the mainThread sets

    while(someCondition && continueRunning)

    {

    //Do work.

    }



  • Patrick Shirley

    You can check to see if a thread is running using the following code:

    myThread.ThreadState & (ThreadState.Stopped | ThreadState.Unstarted);

    Creating another thread in the listview control could be the problem. I am not sure. However, you are trying to implement some sort of thread pooling here.

    There is a great article about "abortable thread pools" in msdn here:

    http://msdn.microsoft.com/netframework/archive/default.aspx pull=/msdnmag/issues/06/03/netmatters/default.aspx

    smc750



  • sarrafi

    Do you check the ThreadState property to see if it is running What is the ThreadState

    smc750



  • Fahd

    If the thread is not running then when you try to abort you are getting the appropriate error message. Apparently, the threads you wanting to abort have already completed. I am not sure you have any problem with the collection.

    smc750



  • DecaysChampion

    Threadstate reports it as "Not Running"..


  • newbieToVJS --Now C#

    Well, how can the thread already be finished with it's work when the download progress for that thread is still updating The thread that is beeing executed triggers a method which invokes a listview control, and that creates a seperate thread, could that be the issue then

    Do you have information regarding thread management like what i'm doing I have been looking on forums, msn, google etc, but i have been unable to dig up information or any classes that has the functionality that i require.

    Class ThreadHandler
    Private Shared Threads as Collection(Of Thread)
    New()
    Abort()
    Pause()
    Resume()
    End Class

    I'd appriciate any information on the subject. ThreadPool, Queue and other classes like that does not offer the functionality that i need.

  • Collection(Of Thread) - Store Thread objects problem