Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Thread procedure with arguments?

Thread procedure with arguments?

Scheduled Pinned Locked Moved Visual Basic
csharpquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hadi Fakhreddine
    wrote on last edited by
    #1

    Hi, I know a little about multi-threading in vb.net I know that we can execute a procedure on a new thread by using: Dim t as New Thread(AddressOf proc) "proc" should be of the form : Sub proc() 'code... End Sub Can i make proc a sub with arguments? like this: Sub proc(str as String) 'code... End Sub and call proc on a new thread and specify str? I mean something like: Dim t as New Thread(AddressOf proc("hello")) If yes, how?

    P D B 3 Replies Last reply
    0
    • H Hadi Fakhreddine

      Hi, I know a little about multi-threading in vb.net I know that we can execute a procedure on a new thread by using: Dim t as New Thread(AddressOf proc) "proc" should be of the form : Sub proc() 'code... End Sub Can i make proc a sub with arguments? like this: Sub proc(str as String) 'code... End Sub and call proc on a new thread and specify str? I mean something like: Dim t as New Thread(AddressOf proc("hello")) If yes, how?

      P Offline
      P Offline
      pbpb
      wrote on last edited by
      #2

      Sub that starts a thread can't have arguments. You should set some kind of property or field before start the thread and use it inside.

      1 Reply Last reply
      0
      • H Hadi Fakhreddine

        Hi, I know a little about multi-threading in vb.net I know that we can execute a procedure on a new thread by using: Dim t as New Thread(AddressOf proc) "proc" should be of the form : Sub proc() 'code... End Sub Can i make proc a sub with arguments? like this: Sub proc(str as String) 'code... End Sub and call proc on a new thread and specify str? I mean something like: Dim t as New Thread(AddressOf proc("hello")) If yes, how?

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        ThreadStart's can't have arguments. Normally, you would put this code into a class, along with some public fields that would represent the parameters for this method you want to start. Then you create and instance of the class, set the fields to the values you need, create your ThreadStart object and point it at the method in your instantiated class, then run it. The method would have to pick up its parameters from the public fields you set in its class.

        Public Class MyThreadClass
        Public argument1 As Integer
         
        Public Sub MyMethod()
        Dim whatever As Integer = argument1
        .
        . your method code...
        .
        End Sub
        End Class
         
        ' Code in your calling class.
        Dim mtc As New MyThreadedClass
        mtc.argument1 = 30
        Dim t As New Thread( AddressOf mtc.MyMethod )

        RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

        1 Reply Last reply
        0
        • H Hadi Fakhreddine

          Hi, I know a little about multi-threading in vb.net I know that we can execute a procedure on a new thread by using: Dim t as New Thread(AddressOf proc) "proc" should be of the form : Sub proc() 'code... End Sub Can i make proc a sub with arguments? like this: Sub proc(str as String) 'code... End Sub and call proc on a new thread and specify str? I mean something like: Dim t as New Thread(AddressOf proc("hello")) If yes, how?

          B Offline
          B Offline
          beowulfagate
          wrote on last edited by
          #4

          You could use a delegate then invoke it asynchronously. Delegates can take arguments unlike threads.

          H 1 Reply Last reply
          0
          • B beowulfagate

            You could use a delegate then invoke it asynchronously. Delegates can take arguments unlike threads.

            H Offline
            H Offline
            Hadi Fakhreddine
            wrote on last edited by
            #5

            Can you please give me a short example? What is a delegate? I would apreciate your help. Thanks in advance

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups