Solved

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

Posted on 2006-06-14
1
243 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Help with my python script 6 145
withoutTen challenge 14 88
pairs challenge 5 44
Help to convert powershell script into a gui 9 74
A short article about a problem I had getting the GPS LocationListener working.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 …

757 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

23 Experts available now in Live!

Get 1:1 Help Now