Solved

In thread creation how to reference a sub within a class file.

Posted on 2006-06-14
1
244 Views
Last Modified: 2010-04-17
I need to use a sub in another class file with a new thread. The name of the class file is CworkerClass and the sub is worker. Below is the code that creates the thread and tries to use the worker sub with no success. I know this must be an easy question for someone. Thanks for looking.


  Dim WorkerThread As Thread = New Thread(AddressOf Worker)

        WorkerThread.Name = "Worker Thread: " & WorkerCount.ToString

        WorkerThread.IsBackground = True
        WorkerThread.Start()
        WorkerCount += 1

        WorkerCountLabel.Text = "Worker Count: " & WorkerCount
0
Comment
Question by:JPERKS1985
1 Comment
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 16902914
I think you mean?...

    Public Class SomeClass

        Public Sub Worker()

        End Sub

    End Class

    Dim sc As New SomeClass
    Dim WorkerThread As Thread = New Thread(AddressOf sc.Worker)

But it would make sense to encapsulate it more with something like..

    Public Class SomeClass

        Private WorkerThread As Thread

        Public Sub StartThread()
            If WorkerThread Is Nothing Then
                WorkerThread = New Thread(AddressOf Me.Worker)
                    WorkerThread.Start()
            End If
        End Sub

        Private Sub Worker()
            ' this thread can access anything in its instance of SomeClass...
            ' so pass any needed parameters for your thread into this intance
        End Sub

    End Class
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is an explanation of a simple data model to help parse a JSON feed
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

911 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

21 Experts available now in Live!

Get 1:1 Help Now