|
-
Nov 22nd, 2002, 12:37 PM
#1
Thread Starter
Addicted Member
How to send information to a thread?
say i want to start a thread from a class called needinfo
but i need to pass on some information(a string) to the thread for it to work properly how do i get the info to the thread? i can do normal threads but when i start trying to pass information to the thread i get stuck and don't know what to do. Thanks for your help!
-
Nov 22nd, 2002, 12:53 PM
#2
Hyperactive Member
You can't pass information directly to the thread but you can pass variables to the class that the thread is in.
Setup properties on the class, and have the thread read the string from that.
-
Nov 22nd, 2002, 04:50 PM
#3
Thread Starter
Addicted Member
what do you mean? like set a
public MyString as string
in the class and then in the form that i am calling the thread from put something like:
dim Thread1 as thread = bla bla bla
thread1.mystring = "whatever i want it to be"
thread1.start
is that what you are talking about?
-
Nov 22nd, 2002, 05:05 PM
#4
I think he means like this:
VB Code:
Public Class ThreadWithParams
Private _Message As String
Public Sub Execute()
Threading.Thread.Sleep(3000)
MsgBox(_Message)
End Sub
Public Sub New(ByVal msg As String)
_Message = msg
End Sub
Public Sub ThreadedExecute()
Dim t As New Threading.Thread(AddressOf Me.Execute)
t.Start()
End Sub
End Class
'Used like this:
Dim tp As New ThreadWithParams(TextBox1.Text)
tp.ThreadedExecute()
'Or
Dim tp As New ThreadWithParams(TextBox1.Text)
dim t as new Threading.Thread(AddressOf tp.Execute)
t.Start()
-
Nov 22nd, 2002, 05:14 PM
#5
Thread Starter
Addicted Member
Thanks you guys have been a big help
-
Nov 25th, 2002, 02:52 PM
#6
Thread Starter
Addicted Member
ok what if i have a form and i want to call more than 1 of the same thread but say i want each of the _message to be different in each thread....pluss i want to be able to have the class raise an event to the form it was called from....how would i need to pass the information to the thread then?
-
Nov 25th, 2002, 03:28 PM
#7
For each one you'd have to make a new instance of the class and thread it. As for the callbacks you could probably just give a delegate to the class that gets invoked as a callback so it just calls the method on the form that you want. That works the same as an event basically.
-
Nov 25th, 2002, 03:32 PM
#8
VB Code:
Public Class Form2
Inherits System.Windows.Forms.Form
'" Windows Form Designer generated code "
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim tp As New ThreadWithParams(TextBox1.Text, AddressOf ShowMessage)
tp.ThreadedExecute()
End Sub
Private Sub ShowMessage(ByVal msg As String)
MsgBox(msg)
End Sub
End Class
Public Class ThreadWithParams
Private _Message As String
Private _del As OnDone
Public Delegate Sub OnDone(ByVal msg As String)
Public Sub Execute()
Threading.Thread.Sleep(3000)
_del.Invoke(_Message)
End Sub
Public Sub New(ByVal msg As String, ByVal cb As OnDone)
_Message = msg
_del = cb
End Sub
Public Sub ThreadedExecute()
Dim t As New Threading.Thread(AddressOf Me.Execute)
t.Start()
End Sub
End Class
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
|