|
-
Jun 12th, 2002, 12:39 PM
#1
Thread Starter
Addicted Member
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
-
Jun 12th, 2002, 12:54 PM
#2
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.
-
Jun 12th, 2002, 01:17 PM
#3
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
-
Jun 12th, 2002, 01:47 PM
#4
Thread Starter
Addicted Member
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
-
Jun 12th, 2002, 02:09 PM
#5
oh..my bad ...ok im stupid...d'uh!!
-
Jun 12th, 2002, 04:10 PM
#6
Thread Starter
Addicted Member
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
-
Jun 12th, 2002, 07:50 PM
#7
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.
-
Jun 13th, 2002, 11:35 AM
#8
Thread Starter
Addicted Member
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
-
Jun 13th, 2002, 02:24 PM
#9
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|