Results 1 to 8 of 8

Thread: How to send information to a thread?

  1. #1

    Thread Starter
    Addicted Member Buy2easy.com's Avatar
    Join Date
    Jul 2002
    Posts
    200

    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!

  2. #2
    Hyperactive Member
    Join Date
    Feb 2001
    Location
    Houston, TX
    Posts
    342
    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.

  3. #3

    Thread Starter
    Addicted Member Buy2easy.com's Avatar
    Join Date
    Jul 2002
    Posts
    200
    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?

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I think he means like this:

    VB Code:
    1. Public Class ThreadWithParams
    2.  
    3.     Private _Message As String
    4.  
    5.     Public Sub Execute()
    6.         Threading.Thread.Sleep(3000)
    7.         MsgBox(_Message)
    8.     End Sub
    9.  
    10.     Public Sub New(ByVal msg As String)
    11.         _Message = msg
    12.     End Sub
    13.  
    14.     Public Sub ThreadedExecute()
    15.         Dim t As New Threading.Thread(AddressOf Me.Execute)
    16.         t.Start()
    17.     End Sub
    18.  
    19. End Class
    20.  
    21. 'Used like this:
    22.         Dim tp As New ThreadWithParams(TextBox1.Text)
    23.         tp.ThreadedExecute()
    24.  
    25. 'Or
    26.         Dim tp As New ThreadWithParams(TextBox1.Text)
    27.         dim t as new Threading.Thread(AddressOf tp.Execute)
    28.         t.Start()

  5. #5

    Thread Starter
    Addicted Member Buy2easy.com's Avatar
    Join Date
    Jul 2002
    Posts
    200
    Thanks you guys have been a big help

  6. #6

    Thread Starter
    Addicted Member Buy2easy.com's Avatar
    Join Date
    Jul 2002
    Posts
    200
    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?

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  8. #8
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    VB Code:
    1. Public Class Form2
    2.     Inherits System.Windows.Forms.Form
    3.  
    4. '" Windows Form Designer generated code "
    5.  
    6.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    7.         Dim tp As New ThreadWithParams(TextBox1.Text, AddressOf ShowMessage)
    8.         tp.ThreadedExecute()
    9.     End Sub
    10.  
    11.     Private Sub ShowMessage(ByVal msg As String)
    12.         MsgBox(msg)
    13.     End Sub
    14.  
    15. End Class
    16.  
    17. Public Class ThreadWithParams
    18.  
    19.     Private _Message As String
    20.     Private _del As OnDone
    21.     Public Delegate Sub OnDone(ByVal msg As String)
    22.  
    23.     Public Sub Execute()
    24.         Threading.Thread.Sleep(3000)
    25.         _del.Invoke(_Message)
    26.     End Sub
    27.  
    28.     Public Sub New(ByVal msg As String, ByVal cb As OnDone)
    29.         _Message = msg
    30.         _del = cb
    31.     End Sub
    32.  
    33.     Public Sub ThreadedExecute()
    34.         Dim t As New Threading.Thread(AddressOf Me.Execute)
    35.         t.Start()
    36.     End Sub
    37.  
    38. 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
  •  



Click Here to Expand Forum to Full Width