Stop Thread until specified action in another thread

  • Thread starter Thread starter scorpion53061
  • Start date Start date
S

scorpion53061

I have MS Word operating in a thread other than the main writing a report.
Can I tell the main thread to wait until a particular point (a sub starts)
in another thread before continuing on?
 
You can use an AutoResetEvent. When you create it the param is True or
False. If it is False then when you do WaitOne it waits. If you create it
with a True param you need to .Reset before you .WaitOne

Imports System.Threading

Public Class Form1
Inherits System.Windows.Forms.Form

Private Shared autoReset As New AutoResetEvent(False)

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Thread.CurrentThread.Name = "GUI Thread"

Debug.WriteLine(Thread.CurrentThread.Name & " started")

Dim workerThread As Thread = New Thread(AddressOf DoSomething)

workerThread.Name = "Worker thread"
workerThread.Start()

Debug.WriteLine(Thread.CurrentThread.Name & " about to wait")

autoReset.WaitOne()

Debug.WriteLine(Thread.CurrentThread.Name & " finishing")

End Sub

Private Sub DoSomething()
Dim i As Integer

Debug.WriteLine(Thread.CurrentThread.Name & " DoSomething")

For i = 1 To 5
Debug.WriteLine(Thread.CurrentThread.Name & " sleeping...")
Thread.CurrentThread.Sleep(500)
Next
Debug.WriteLine(Thread.CurrentThread.Name & " exit DoSomething")
autoReset.Set()
End Sub
End Class
 
Back
Top