Solved

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

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

A short article about a problem I had getting the GPS LocationListener working.
This is an explanation of a simple data model to help parse a JSON feed
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

809 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