Passing Parameter to Thread

In .NET 2.0, there is a new delegate, ParameterizedThreadStart, which takes
a parameter of type object. You can create a thread using an instance of
this delegate instead of just ThreadStart, and a new overload to Thread.Start
allows you to specify the value to be passed to the new thread. This is simple, but only accepts
a single parameter and isn’t type-safe (just like the options when using thread pool threads).
The earlier code could then be rewritten as:

[In some method or other]

Thread t = new Thread (new ParameterizedThreadStart(FetchUrl));

t.Start (myUrl);



[And the actual method...]

static void FetchUrl(object url)

{

// use url here, probably casting it to a known type before use

}



http://www.yoda.arachsys.com/csharp/threads/parameters.shtml

powered by performancing firefox

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: