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 FolderCompressor();
fc.Folder = @"C:\MyFolder";
Thread compressThread =
Thread (new ThreadStart (fc.Compress));
compressThread.Start();
ParameterizedThreadStart lets you specify the thread's parameter when you start the thread, thereby allowing you to move the object's processing to a static method, like so:
// An object that compresses the contents of a folder to
// a .ZIP file.
public class FolderCompresser
{
// The compress method
public static void Compress
(Object o)
{
string folder = o as string;
string zipFilename = folder + ".zip";
internalCompress (folder, zipFilename);
}
}
The client code that uses FolderCompressor is reduced to:
Thread compressThread =
new Thread (new ParameterizedThreadStart
(FolderCompresser.Compress));
compressThread.Start (@"C:\MyFolder");
And there you have it - a useful helper class to make your managed multithreaded code more manageable.
Happy Multi-Threading!
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 FolderCompressor();
fc.Folder = @"C:\MyFolder";
Thread compressThread =
Thread (new ThreadStart (fc.Compress));
compressThread.Start();
ParameterizedThreadStart lets you specify the thread's parameter when you start the thread, thereby allowing you to move the object's processing to a static method, like so:
// An object that compresses the contents of a folder to
// a .ZIP file.
public class FolderCompresser
{
// The compress method
public static void Compress
(Object o)
{
string folder = o as string;
string zipFilename = folder + ".zip";
internalCompress (folder, zipFilename);
}
}
The client code that uses FolderCompressor is reduced to:
Thread compressThread =
new Thread (new ParameterizedThreadStart
(FolderCompresser.Compress));
compressThread.Start (@"C:\MyFolder");
And there you have it - a useful helper class to make your managed multithreaded code more manageable.
Happy Multi-Threading!
Comments