Passing Parameters

In many cases you might have the need to pass data to new threads. This can be accomplished by creating an instance of the ParameterizedThreadStart delegate, which requires an argument of type Object that you can use for sharing your data. The following code demonstrates how you create a thread with parameters:

image

Notice how the Thread.Start method has an overload that takes the specified parameter as the data. Because such data is of type Object, you need to convert it into the most appropriate format. The following code demonstrates how to implement a method that the delegate refers to and how to convert the data into a hypothetical string:

image

Of course you can take advantage of lambda expressions if you do not want to provide an explicit delegate also in this kind of scenario.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.177.115