|
-
Sep 28th, 2009, 09:35 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Thread argument
Is it possible to pass an argument to a thread and if so how?
-
Sep 28th, 2009, 09:44 AM
#2
Re: Thread argument
You mean when you are creating the thread? If so then yeah you can do it like this:
vb Code:
Dim newthread As New Threading.Thread(AddressOf ThreadWork)
newthread.IsBackground = True
newthread.Start("hello") 'So the string 'hello' is our argument that we are passing in
'This is the sub that will be executed on the background thread
Private Sub ThreadWork(ByVal Argument As Object)
Dim ArgString As String = CStr(Argument)
'ArgString now contains the string that was passed in to the thread
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)
-
Sep 28th, 2009, 09:58 AM
#3
Thread Starter
Addicted Member
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)
-
Sep 28th, 2009, 10:01 AM
#4
Thread Starter
Addicted Member
Re: Thread argument
OK I understand now - My thread was not expecting the object.
Many thanks for your help
-
Sep 28th, 2009, 10:02 AM
#5
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
-
Sep 28th, 2009, 10:02 AM
#6
Re: [RESOLVED] Thread argument
Ah, looks like you got it anyway
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
|