Home All Groups Group Topic Archive Search About

Threading so restricted?

Author
6 May 2006 1:15 PM
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

Author
6 May 2006 1:49 PM
Herfried K. Wagner [MVP]
"Adam Honek" <AdamHo***@Webmaster2001.freeserve.co.uk> schrieb:
> 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/>
Author
6 May 2006 5:50 PM
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

Show quoteHide quote
"Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
news:%23fegqPRcGHA.1856@TK2MSFTNGP03.phx.gbl...
> "Adam Honek" <AdamHo***@Webmaster2001.freeserve.co.uk> schrieb:
>> 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/>
Author
8 May 2006 12:25 PM
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


Show quoteHide quote
"Adam Honek" <AdamHo***@Webmaster2001.freeserve.co.uk> wrote in message
news:OWG5KWTcGHA.3344@TK2MSFTNGP03.phx.gbl...
| 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
|
| "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
| news:%23fegqPRcGHA.1856@TK2MSFTNGP03.phx.gbl...
| > "Adam Honek" <AdamHo***@Webmaster2001.freeserve.co.uk> schrieb:
| >> 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/>
|
|
Author
6 May 2006 5:10 PM
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

--
Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.