TPL - Using static method vs struct method
Posted
by Sunit
on Stack Overflow
See other posts from Stack Overflow
or by Sunit
Published on 2010-04-30T14:23:20Z
Indexed on
2010/04/30
14:27 UTC
Read the original article
Hit count: 177
c#
I have about 1500 files on a share for which I need to collect FileVersionInfo string. So I created a Static method in my Gateway like this:
private static string GetVersionInfo(string filepath)
{
FileVersionInfo verInfo = FileVersionInfo.GetVersionInfo(filepath);
return string.Format("{0}.{1}.{2}.{3}", verInfo.ProductMajorPart, verInfo.ProductMinorPart,
verInfo.ProductBuildPart, verInfo.ProductPrivatePart).Trim();
}
And then used FileAndVersion struct in a PLINQ call with DegreeOfParallelism as this is I/O related
resultList = dllFilesRows.AsParallel().WithDegreeOfParallelism(20)
.Select(r =>
{
var symbolPath = r.Filename;
return new FilenameAndVersion{Filename=symbolPath, Version=GetVersionInfo(symbolPath)};
})
.ToArray();
Later I modified the Struct, FileAndVersion as:
private struct FilenameAndVersion
{
private string _version, _filename;
public string Version { get { return _version; } }
public string Filename { get { return _filename; } }
public void SetVersion()
{
FileVersionInfo verInfo = FileVersionInfo.GetVersionInfo(this.Filename);
this._version = string.Format("{0}.{1}.{2}.{3}", verInfo.ProductMajorPart, verInfo.ProductMinorPart,
verInfo.ProductBuildPart, verInfo.ProductPrivatePart).Trim();
}
public FilenameAndVersion(string filename, string version)
{
this._filename = filename;
this._version = string.Empty;
SetVersion();
}
}
And used it:
resultList = dllFilesRows.AsParallel().WithDegreeOfParallelism(20)
.Select(r =>
{
var symbolPath = r.Filename;
return new FilenameAndVersion(symbolPath, String.Empty);
})
.ToArray();
The question is, is this going to help me in anyway and is a good pattern to use ?
Sunit
© Stack Overflow or respective owner