Jump to content

Threading Problem

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
T-Wave

T-Wave

    Newbie

  • Members
  • Pip
  • 7 posts
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?

#2
LCD

LCD

    Newbie

  • Members
  • PipPip
  • 15 posts
wat is this..

#3
Dyroxide

Dyroxide

    Learning Programmer

  • Members
  • PipPipPip
  • 39 posts
First of all this is your code:

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

you'll have to specify more of what you want your program to do but the "<>" that your using is like a less than or greater than symbol added where its saying this is not this

so your code would say this

While ThreadX.ThreadState is not
System.Threading.ThreadState.Aborted AndAlso ThreadX.ThreadState is not
System.Threading.ThreadState.Stopped Then
System.Threading.Thread.Sleep(1)
End While

Return ret

for this code to even remotely work you would need "return ret" inside of the while

its just like an if statement

if this = this then

do this

else

do this

end if

your code should look like this:

Public Class myClass

    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)

            Return ret

        End While

    End Function


    Public Sub doSomethingElse()

        msgbox("this is the doSomethingElse() sub")

    End Sub

    Public Sub closeSomething()

        msgbox("this is the closeSomething() sub")

    End Sub

End Class

you have to put somethin in the "doSomethingElse()" sub before you can call it from another sub, your basically saying

Dim ThreadX As New System.Threading.Thread(AddressOf NOTHING)

you cant dim something as nothing

also let me in on the reason you had the code:

ThreadX.IsBackground = True

ThreadX.Start()

i believe thats saying.. if doSomethingElse() is background then start thread doesnt make much sense

here is some example code for you to build from:

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

Dim newthread As System.Threading.Thread

        newthread = New Threading.Thread(AddressOf DanceLEDS)

        newthread.Start()

Sub DanceLEDS()

Do process.start("notepad.exe")

        Loop


    End Sub