Results 1 to 9 of 9

Thread: Understanding Multithreading

  1. #1

    Thread Starter
    Addicted Member Dmyze's Avatar
    Join Date
    Mar 2002
    Location
    Seattle
    Posts
    160

    Question Understanding Multithreading

    Ok, I got the basic concept down:

    Sub LoopThread()

    'Code for Thread

    End Sub

    Sub Main()

    Dim myThread As Thread

    myThread = New Thread(AddressOf LoopThread)
    myThread.Start()

    End Sub


    What I want to know is if you can pass the LoopThread any variables? I seem to get an error message whenever I try:

    Sub LoopThread(ByVal x As Short)
    'Code
    end Sub

    I get a blue line under LoopThread on the following line:

    myThread = New Thread(AddressOf LoopThread)

    Something about the wrong signature...

    This didn't work either:
    myThread = New Thread(AddressOf LoopThread(x))

    Is the only way to pass it a variable by using a global? I hope not.
    -Daryl
    "Two More Rolls of Duct tape, and the world is mine!"
    VB.NET Guru

  2. #2
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    myThread = New Thread(AddressOf LoopThread(x))
    I would think that that should work: What error do you get there? Make sure you are actually passing in a short.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  3. #3
    Tygur
    Guest
    You can only pass a sub that takes no arguments into the constructor for the Thread class, as you noticed. This is because it has to match the ThreadStart delegate, which is declared like this:
    Public Delegate Sub ThreadStart()

    Only Subs that match that delegate, and therefore take no arguments, are accepted.

    You can still pass information into your thread, however. One way that is recommended by the .NET documentation is to create a separate class for your new thread. Here's an example of what I'm talking about:
    Code:
        Shared Sub Main()
            Dim C As New TheNewClassForTheThread()
            Dim T As New System.Threading.Thread(AddressOf C.ThreadSub)
            C.Text = "This message is being shown from a different thread."
            T.Start()
        End Sub
    
        'You might want to give this class a shorter name
        Class TheNewClassForTheThread
            Public Text As String
            Public Sub ThreadSub()
                MsgBox(Text)
            End Sub
        End Class

  4. #4

    Thread Starter
    Addicted Member Dmyze's Avatar
    Join Date
    Mar 2002
    Location
    Seattle
    Posts
    160
    Excellent...

    I have been going over the MSDN Document that does what you did, but their code made my head hurt, thanks for the simple example.
    -Daryl
    "Two More Rolls of Duct tape, and the world is mine!"
    VB.NET Guru

  5. #5
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    oh..my bad ...ok im stupid...d'uh!!
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  6. #6

    Thread Starter
    Addicted Member Dmyze's Avatar
    Join Date
    Mar 2002
    Location
    Seattle
    Posts
    160
    Ok, So now how do I get the data out of the Threads?

    Here is the Class I use:


    Code:
        Class cThread
            Public rs As ADODB.Recordset
            Public oFolder As Scripting.Folder
            Public Complete As Boolean = False
            Public FoundFile As Boolean = False
            Private oFiles As Scripting.Files
            Public Sub ThreadSub()
                Search_Files(rs, oFolder)
                Complete = True
                'rs.filter to set FoundFile Value
            End Sub
        End Class
    
    
    'CODE that calls the Threads:
    
        Private iThread As Integer
        Private tFolder As System.Threading.Thread()
    
    
    sub startThreads()
    
     'create rs with a list of files
    
            iThread = iThread + 1
            ReDim tFolder(iThread - 1)
            tFolder(iThread - 1) = New System.Threading.Thread(AddressOf oThread.ThreadSub)
            tFolder(iThread - 1).Name = Str(iThread - 1)
            oThread.rs = rs
            oThread.oFolder = oFolder
            tFolder(iThread - 1).Start()
    
    end sub
    
    
    sub endThreads()
    
        MsgBox(iThread)
    
    Dim i As Integer
    Dim bDone As Boolean
    Dim oThread As cThread
    
        For i = 0 To iThread
    
          'If tFolder(iThread).GetData("A") = True Then ??????
          'What code should I use to check to make cure all threads Complete status = True???
    
          End If
    
        Next
    
        Stop
    -Daryl
    "Two More Rolls of Duct tape, and the world is mine!"
    VB.NET Guru

  7. #7
    Tygur
    Guest
    You can determine whether a thread is still running by checking the IsAlive property of the Thread object. If it's true, the thread is still running. If it's false, it's done.

    Does that help any? I didn't get a very good look at your code.

  8. #8

    Thread Starter
    Addicted Member Dmyze's Avatar
    Join Date
    Mar 2002
    Location
    Seattle
    Posts
    160
    Once the tread is done I want to look at some of it's properties, like I have the variable FoundFile will be "True" if the thread found the file I was looking for, and then if it's True I want to look at the Recordset to see which file it found.
    Last edited by Dmyze; Jun 13th, 2002 at 02:21 PM.
    -Daryl
    "Two More Rolls of Duct tape, and the world is mine!"
    VB.NET Guru

  9. #9

    Thread Starter
    Addicted Member Dmyze's Avatar
    Join Date
    Mar 2002
    Location
    Seattle
    Posts
    160
    I think i figured it out, I'm creating an array of the Class and then executing the thread. My code is really whacking out now with wired error messages and very unexpected results.. I'm re-writing the app again now That I better understand how Multi-Threading works, I'll post more If I have problems with the new app.
    -Daryl
    "Two More Rolls of Duct tape, and the world is mine!"
    VB.NET Guru

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width