I have some difficulties concerning threading and hope you guys could be of help.
Let's say I have the following class:
Public Class myClass Public Sub closeSomething() End Sub Public Sub doSomethingElse() End Sub Public Function doSomething() As String Dim ret As String = "Blablabla" Dim ThreadX As New System.Threading.Thread(AddressOf doSomethingElse) ThreadX.IsBackground = True ThreadX.Start() While ThreadX.ThreadState <> System.Threading.ThreadState.Aborted AndAlso ThreadX.ThreadState <> System.Threading.ThreadState.Stopped System.Threading.Thread.Sleep(1) End While Return ret End Function End Class
Let's say I use the above class in the following way:
1- Dim str As String 2- Dim x As New myClass 3- str = x.doSomething() 4- x.closeSomething()
I've included line numbers so I can just refer the lines by their numbers.
My problem is, when the program executes, line 4 seems to be executed earlier than I want it to. It gets executed while ThreadX is still running in line 3. How can I force the program to wait until ThreadX has finished executing, before it executes line 4?