0

i have a thread which produces multiple threads in it. how can i be sure if all the threads are completed? i have to call a method after all the threads are finished. this is not a duplicate like

dim thread1.start() dim thread2.start() if (all threads are finished) muFunc() end if 

you can see the sample code snippet below. see the code here

that is where i am stuck badly. what should i do? thanks in advance.

5
  • 1
    Possible duplicate of How to check if Thread finished execution Commented Jan 10, 2018 at 13:53
  • 1
    In this example, it's probably easiest to just call Join on both threads. Commented Jan 10, 2018 at 14:12
  • 1
    Don't post pictures of code. Commented Jan 10, 2018 at 19:12
  • @larsTech. then why is the option given for the image upload? only for showing the result or DB changes ?? Commented Jan 11, 2018 at 8:06
  • If you look around at other questions, code is almost always in text form. This makes it easier for us to copy the code and test the problem. The option to upload images is for screen shots etc to help explain the problem better. Commented Jan 11, 2018 at 15:04

2 Answers 2

0

Call this method from wherever you want (Form.Load, Button.Click...)
The Caller Event Handler/Method doesn't need to be an Async one

ThreadPoolManager() 


Define a scope for the List(Of Thread) that holds the Thread Pool

Dim ThreadPool As List(Of Thread) 


ThreadPoolManager() (a)waits StartThreads(), which starts all the Threads in the ThreadPool List. When the Async method returns, then calls MyFunctionWhenAllThreadsAreFinished()

 Private Async Sub ThreadPoolManager() If Await StartThreads() = True Then MsgBox("All Finished!", MsgBoxStyle.OkOnly, "Finally!") Call MyFunctionWhenAllThreadsAreFunished() Else MsgBox("Something went wrong!", MsgBoxStyle.OkOnly, "Damn it!") End If End Sub 

The StartThreads() method initializes all the Threads in ThreadPool with the delegate worker method to invoke when the thread starts

 Protected Async Function StartThreads() As Task(Of Boolean) ThreadPool = New List(Of Thread) Dim MyThread1 As Thread = New Thread(AddressOf Me.MyThread1Work) Dim MyThread2 As Thread = New Thread(AddressOf Me.MyThread2Work) ThreadPool.AddRange({MyThread1, MyThread2}) For Each _PT As Thread In ThreadPool : _PT.Start() : Next Return Await Task.Run(Function() Dim AllFinished As Integer While AllFinished < ThreadPool.Count AllFinished = 0 For Each _T As Thread In ThreadPool If _T.IsAlive = False Then AllFinished += 1 Next End While Console.WriteLine("All Thread Finished") Return True End Function) End Function Private Sub MyThread1Work(_Obj As Object) Thread.Sleep(5000) Console.WriteLine("Thread 1 Finished") End Sub Private Sub MyThread2Work(_Obj As Object) Thread.Sleep(7000) Console.WriteLine("Thread 2 Finished") End Sub 
Sign up to request clarification or add additional context in comments.

Comments

0

Use the Thread.IsAlive flag. This is to give the thread status.

(copied from the duplicate question)

(whew, I don't have the time to reverse engineer that previous answer to understand how it works.)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.