I have a Parallel foreach function that creates a new instance of a class, that manipulates a picture, and saves it to the disk…
However approximately 4 times out of 400, the picture gets saved to the disk, but without being manipulated, my theory is that when it happens, some of the propperties existing in my class is null, when they are not suppost to…
The 4 (sometimes 3) errors mostly occurs in the first 10 images of the parallel loop.
There is no error message, it justs skip some of my code, for some reason… My breakpoint doesn’t work when it is parralel, so it is hard to debug.
Any advice on how to proceed / debug / fix ?
The code as requested
private static void GenerateIcons(Effects effect)
{
DirectoryInfo dir = new DirectoryInfo(HttpContext.Current.Server.MapPath(@"~\Icons\Original\"));
FileInfo[] ff = dir.GetFiles();
string mappath = HttpContext.Current.Server.MapPath(@"~\Icons\");
List<string> paths = new List<string>();
string ids = GetAllEffectIds(effect.TinyUrlCode);
Parallel.ForEach(ff, item =>
{
if (!File.Exists(mappath + @"Generated\" + ids + "-" + item.Name))
{
paths.Add(mappath + @"Generated\" + ids + "-" + item.Name);
ApplyEffects f = new ApplyEffects(effect, item.Name, mappath);
f.SaveIcon();
}
});
//Zip icons!
ZipFiles(paths, effect.TinyUrlCode, ids, effect.General.Prefix);
}
You can re-write it in a more functional style, to hopefully remove the threading issues: