How to create a Thread Handler

Posted on 2007-10-05
Last Modified: 2008-04-09
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???

Question by:DavidGreenfield
    1 Comment
    LVL 85

    Accepted Solution

    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"...

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now