Solved

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

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
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…
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…
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 …

803 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