Results 1 to 6 of 6

Thread: [RESOLVED] Thread argument

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Location
    Devon - UK
    Posts
    214

    Resolved [RESOLVED] Thread argument

    Is it possible to pass an argument to a thread and if so how?

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Thread argument

    You mean when you are creating the thread? If so then yeah you can do it like this:

    vb Code:
    1. Dim newthread As New Threading.Thread(AddressOf ThreadWork)
    2. newthread.IsBackground = True
    3. newthread.Start("hello") 'So the string 'hello' is our argument that we are passing in
    4.  
    5. 'This is the sub that will be executed on the background thread
    6. Private Sub ThreadWork(ByVal Argument As Object)
    7.         Dim ArgString As String = CStr(Argument)
    8.         'ArgString now contains the string that was passed in to the thread
    9. End Sub
    You have to declare the argument as type Object for it to work, so the first thing I usually do in my threads' work sub is to declare some new variables that are of the correct type and convert them using DirectCast (or something like CStr as I have done in the example above)
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Location
    Devon - UK
    Posts
    214

    Re: Thread argument

    Chris

    I thought that was how to do it but with the following I'm getting:-

    HTML Code:
     Error	1	Overload resolution failed because no accessible 'New' can be called with these arguments:
        'Public Sub New(start As System.Threading.ParameterizedThreadStart)': Option Strict On does not allow narrowing in implicit type conversions between method 'Public Sub GetMissingTile(i As Integer)' and delegate 'Delegate Sub ParameterizedThreadStart(obj As Object)'.
        'Public Sub New(start As System.Threading.ThreadStart)': Method 'Public Sub GetMissingTile(i As Integer)' does not have a signature compatible with delegate 'Delegate Sub ThreadStart()'.	C:\Dot Net 2008 Code\BSGPSV3524\Common.vb	1022	42	BSGPS
    and I don't understand the error message

    Here is my code:-

    HTML Code:
     Dim TileDownload As Thread = New Thread(AddressOf MyGPSForm.OSMTiledownload.GetMissingTile)
                TileDownload.Priority = ThreadPriority.Lowest
                '
                '  Fire the thread
                '
                TileDownload.Start(0)

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Location
    Devon - UK
    Posts
    214

    Re: Thread argument

    OK I understand now - My thread was not expecting the object.

    Many thanks for your help

  5. #5
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [RESOLVED] Thread argument

    Read my original post again, specifically this part:
    You have to declare the argument as type Object for it to work
    So change your GetMissingTile method to accept an argument of type Object instead of Integer as you are currently doing
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  6. #6
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [RESOLVED] Thread argument

    Ah, looks like you got it anyway
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


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