Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7497045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T18:54:27+00:00 2026-05-29T18:54:27+00:00

I have a MultiThreading issue and conceptual question using Visual Basic (but it applies

  • 0

I have a MultiThreading issue and conceptual question using Visual Basic (but it applies to almost all languages).
I have a function which:

  1. creates 5 instances of a class which each spawns a thread which calls a function
  2. The Function sleeps for X milli-seconds
  3. prints to console
  4. then terminates the thread

How can I ensure all instances of the class gets a chance to finish before main cuts it off??

Most relevant help through research: How do I pause main() until all other threads have died?

Here is my code:

Imports System
Imports System.Threading
Imports System.Collections.Generic
Module Module1
    Private raceTrack As New List(Of RaceCar)
    Sub Main()
        CreateRace() 
        'Main() would quit when the 5 car objects are created =[
        'How do I make sure Main will not exit until all racecars are done?
    End Sub

    Sub CreateRace()
        For index As Integer = 1 to 5
            raceTrack.Add(new RaceCar(index))
        Next
    End Sub

    Public Class RaceCar
        Private testThread as Thread =  New Thread(AddressOf StartEngine)
        Private carId as Integer
        Public Sub New(ByVal carNumber as Integer)
            me.carId = carNumber
            Me.TestThread.Start()
            Me.TestThread.Join()
        End Sub

        Public Sub StartEngine()
            Console.WriteLine(String.Format("Car#{0} is going!", Me.carId))
            Thread.Sleep(100*Me.carId)
            Console.WriteLine(String.Format("Car#{0} is DONE!", Me.carId))
        End Sub

    End Class

End Module
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T18:54:29+00:00Added an answer on May 29, 2026 at 6:54 pm

    Apparently your RaceCar constructor only returns when the spawned thread finishes, since you are calling Join.

        Public Sub New(ByVal carNumber as Integer)
            me.carId = carNumber
            Me.TestThread.Start()
            Me.TestThread.Join()
        End Sub
    

    I don’t know if this is intentional (most likely not, because you are creating your RaceCar instances in a loop, so you have no parallelism, since the next instance cannot be created until the previous constructor call returns), but definitely your main thread cannot finish until all the child threads have finished.

    I suggest the following restructuring:

    Remove the Join from the constructor and add a new method in your RaceCar class:

    Public Sub JoinThread()
        Me.TestThread.Join()
    End Sub
    

    Now the object creation loop will not block. Add another loop afterwards:

    Sub CreateRace()
        For index As Integer = 1 to 5
            raceTrack.Add(new RaceCar(index))
        Next
    
        // other work maybe?
    
        For Each car As RaceCar In raceTrack
            car.JoinThread()
        Next
    End Sub
    

    Now your threads will start in the first loop, run their work in parallel and eventually stop on the new “barrier loop”.

    Note that the main thread can still do other tasks between the two loops.

    (Disclaimer: I’ve never programmed in vb.net, so hopefully what I wrote is not totally wrong.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application which performs 30 independent tasks simultaneously using multithreading, each task
Have just started using Visual Studio Professional's built-in unit testing features, which as I
I need clarification regarding an issue related to multithreading. I have threads which acquires
I have a basic cs-major understanding of multi-threading but have never had to do
I have a set of operations that are very expensive but are all pretty
I have a very bad feeling about using lock in my code but now
I have the following multithreading function to implement threads fetching from a list of
I have never come across multithreading but I hear about it everywhere. What should
I have a extension that uses multithreading using worker thread as shown here .
I have a GUI that is for all intents and purposes really basic. A

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.