How to create a Thread Handler

How can I create an array? of threads that are accessible from forms and classes within my application??

I am putting

Public thread_handler() As Thread

in my form and adding threads using

        Dim count As New Integer
        If thread_handler Is Nothing Then count = 0 Else count = UBound(thread_handler) + 1
        ReDim Preserve thread_handler(count)
        thread_handler(count) = t 'add thread to thread handler

but is this correct and is there a better way as I am having some problems with checking threads (where activex objects aren't returning information)  ie.

Error: COM object that has been separated from its underlying RCW cannot be used.

System.Runtime.InteropServices.InvalidComObjectException: COM object that has been separated from its underlying RCW cannot be used

and I think its because of the way I am handling them???

Who is Participating?
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
Use a Public Shared List to store references to your threads.  This can be accessed from ANYWHERE within your application:

    Public Class MyThreads
        Public Shared Threads As New List(Of Threading.Thread)
    End Class

To use it:

    Dim t As New Thread(AddressOf c.Connect) 'Declare a thread to run connect actions on the connection
    MyThreads.Threads.Add(t) ' <---- adding your thread to the Shared List

You can enumerate the threads with:

    For Each T As Thread In MyThreads.Threads
        ' do something with "T"...
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.