Greetings all,

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?

Recommended Answers

All 6 Replies

That's normal because you don't stop the main thread, your process or application in line 3 has 2 threads working in parallel..

Thanks for your reply RamyMahrous, but how do I stop the main thread?

I was thinking that the following section:

While ThreadX.ThreadState <> System.Threading.ThreadState.Aborted AndAlso ThreadX.ThreadState <> System.Threading.ThreadState.Stopped
                    System.Threading.Thread.Sleep(1)
          End While

would ensure that the function doSomething() doesn't return until ThreadX finishes running, and that line 4 therefore would not be executed before. That's obviously not the case, but what am I doing wrong? And more importantly, how do I do it right?

My opinion is to remove the initiation of the new thread in the class

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

rather creating this new thread in the class you call DoSomething

1- Dim str As String
2- Dim x As New myClass
3- Dim thrd As new Thread(new ThreadStart(x.DoSomthing)
' Try to modify DoSomthing to be void to work in this wasy
'4- x.closeSomething()

Or you can read more about threading, talk to thread from another, etc.. it may help http://msdn.microsoft.com/en-us/library/system.threading.thread_members.aspx

Thanks for your suggestion, RamyMahrous... I was able to circumvent the problem somehow by having the instantiation of myClass in its own thread. No idea why that worked.

Let me see to learn from you, and if it solved please mark it as solved to be used as reference for people facing this problem. Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.