Threading so restricted?

A

Adam Honek

Hi all,

Is it honestly true we can't start threads in VB.net 2005 if the sub or
function has paratemeters like functioname(x1 as string, x2 as long) etc?

This so restricts launching new threads.

Global variables the only option?

We can't use TLS before launching the thread right?

Thanks a lot,
Adam
 
H

Herfried K. Wagner [MVP]

Adam Honek said:
Is it honestly true we can't start threads in VB.net 2005 if the sub or
function has paratemeters like functioname(x1 as string, x2 as long) etc?

Check out the 'ParameterizedThreadStart' delegate.
 
M

Mattias Sjögren

Adam,
This so restricts launching new threads.

Global variables the only option?

I don't think it's a big restriction. A common solution is to
encapsulate the thread logic in a separate class, pass the "thread
arguments" to a class constructor and store them in fields. No global
variables needed.


Mattias
 
A

Adam Honek

Hmmm, looking at some code this only seems to be doable if the sub is in a
class.

Can't see anything if the sub is in a module,

Adam
 
J

Jay B. Harlow [MVP - Outlook]

Adam,
| Hmmm, looking at some code this only seems to be doable if the sub is in a
| class.
Look again, the target of a Delegate can be in a Module, Structure, or a
Class. It can also be a shared method of a Class or Structure.

For example:

Public Module MainModule

Private Sub Work(ByVal obj As Object)
Dim parameter As String = TryCast(obj, String)
... do stuff based on parameter ...
End Sub

Public Sub Main()
Dim worker As New Thread(AddressOf work)
Dim parameter As String = "Something"
worker.Start(parameter)
... wait for thread to finish ...
End Sub

End Module

Notice that the sub is in a Module. If I needed to pass more then one
parameter, I would consider passing a structure or a class...

NOTE: 'ParameterizedThreadStart' is new to .NET 2.0 (VS 2005)

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Hmmm, looking at some code this only seems to be doable if the sub is in a
| class.
|
| Can't see anything if the sub is in a module,
|
| Adam
|
| | >> Is it honestly true we can't start threads in VB.net 2005 if the sub or
| >> function has paratemeters like functioname(x1 as string, x2 as long)
etc?
| >
| > Check out the 'ParameterizedThreadStart' delegate.
| >
| > --
| > M S Herfried K. Wagner
| > M V P <URL:http://dotnet.mvps.org/>
| > V B <URL:http://classicvb.org/petition/>
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top