Parameterized thread start – C#

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

}

zp8497586rq
Tags: , , ,

Comments are closed.