Solved

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

Posted on 2006-06-14
1
247 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 86

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?

726 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