Skip to main content

Posts

Showing posts from December, 2009

Thread Parameters

One of the useful niblets in .NET 2.0 is the new ParameterizedThreadStart delegate that makes it convenient to pass parameters (OK, a single parameter - but it's an Object so you can pretty much pass anything you want!) to a thread. ParameterizedThreadStart makes your multithreaded code more readable and therefore easier to maintain. In the "old" days, I had to expose an explicit property in the class that performed a multithreaded task like so: // An object that compresses the contents of a folder to // a .ZIP file. public class FolderCompresser { // The folder to be compressed to a .ZIP file public string Folder { get { return _folder; } set { _folder = value; } } // The compress method public void Compress() { string zipFilename = _folder + ".zip"; internalCompress (_folder, zipFilename); } } The client code that used FolderCompresser had to explicitly set its parameter before starting the thread: // Invoke the compressor FolderCompressor fc = new FolderComp