Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 不可靠的并行循环400次中有4次失败_C#_Asp.net Mvc_Parallel Processing - Fatal编程技术网

C# 不可靠的并行循环400次中有4次失败

C# 不可靠的并行循环400次中有4次失败,c#,asp.net-mvc,parallel-processing,C#,Asp.net Mvc,Parallel Processing,我有一个并行foreach函数,它创建一个类的新实例,处理一幅图片,并将其保存到磁盘上 然而,大约400次中有4次,图片被保存到磁盘上,但没有被操作,我的理论是,当它发生时,我的类中存在的一些属性是空的,当它们不受支持时 4个(有时3个)错误主要发生在并行循环的前10个图像中 没有错误消息,只是因为某些原因跳过了我的一些代码。。。我的断点在parralel时不起作用,因此很难调试 有关于如何继续/调试/修复的建议吗 按要求输入代码 private static void Generate

我有一个并行foreach函数,它创建一个类的新实例,处理一幅图片,并将其保存到磁盘上

然而,大约400次中有4次,图片被保存到磁盘上,但没有被操作,我的理论是,当它发生时,我的类中存在的一些属性是空的,当它们不受支持时

4个(有时3个)错误主要发生在并行循环的前10个图像中

没有错误消息,只是因为某些原因跳过了我的一些代码。。。我的断点在parralel时不起作用,因此很难调试

有关于如何继续/调试/修复的建议吗

按要求输入代码

    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);

    }
私有静态无效生成(效果)
{
DirectoryInfo dir=newdirectoryinfo(HttpContext.Current.Server.MapPath(@“~\Icons\Original\”);
FileInfo[]ff=dir.GetFiles();
字符串mappath=HttpContext.Current.Server.mappath(@“~\Icons\”);
列表路径=新列表();
字符串ID=getAllEffectID(effect.TinyUrlCode);
Parallel.ForEach(ff,项目=>
{
如果(!File.Exists(映射路径+@“生成的\”+ids+“-”+item.Name))
{
添加(mappath+@“生成的\”+ids+“-”+item.Name);
ApplyEffects f=新的ApplyEffects(效果、项名称、映射路径);
f、 SaveIcon();
}
});
//拉链图标!
ZipFiles(路径、effect.TinyUrlCode、id、effect.General.Prefix);
}
  • 你检查过非并行版本吗

  • 你在用什么 未标记为线程安全的API函数

  • 为了回答1),互斥锁锁定整个函数并测试错误

    回答2)减少互斥体中的代码量,直到找到有问题的函数。你可以通过二等分来完成


    ConcurrentBag
    是一个线程安全的容器。

    我的理论是,由于
    list
    不是线程安全的,所以您的路径列表没有正确更新。基本上,如果两个线程同时尝试向列表中添加一个项目,可能会发生很多奇怪的事情,比如结果列表中缺少4个项目。尝试使用


    您可以以更实用的方式重新编写,希望能够消除线程问题:

    private static void GenerateIcons(Effects effect)
    {
        var dir     = new DirectoryInfo(HttpContext.Current.Server.MapPath(@"~\Icons\Original\"));
        var mappath = HttpContext.Current.Server.MapPath(@"~\Icons\");
        var ids     = GetAllEffectIds(effect.TinyUrlCode);
    
        var filesToProcess = dir
            .EnumerateFiles()
            .AsParallel()
            .Select(f => new { info = f, generated = File.Exists(mappath + @"Generated\" + ids + "-" + f.Name) })
            .ToList();
    
        Parallel.ForEach(filesToProcess.Where(f => !f.generated), file =>
        {
            new ApplyEffects(effect, file.info.Name, mappath).SaveIcon();
        });
    
        //Zip icons!
        ZipFiles(filesToProcess.Select(f => f.info), effect.TinyUrlCode, ids, effect.General.Prefix);
    }
    

    我会修改代码的第4行,这应该会修复它。@ChaosPandion,我个人会在第400行这样做。第4行在我看来是正确的:-)@ChaosPandion哈哈xD,但它是空的!我将用一些代码更新它:p@BjarkeCK,什么/谁是空的?罐子?来吧,伙计,在这个问题结束之前问一个真正的问题。谢谢你,阿温瑟现在会更仔细地研究它,是的,它可以与非并行版本一起工作。。。快速提问,线程安全?如果没有适当的互斥锁,从不同线程访问相同数据的函数可能不是线程安全的。很遗憾,锁(路径)没有工作,但我正在调查ApplyEffects中的代码,看看是否有任何代码不是线程安全的。谢谢你的提示。@Bjareck-尽管这不是一个解决方案,但你应该熟悉线程安全集合。哇,在这里你可以学到很多其他东西:)但是这段代码实际上将错误数增加到了40个左右。。。如果我锁定效果,并将其环绕在parralel中的所有内容上,它不会出现任何错误,但它的速度与非平行foreach循环一样慢。“应用效果”的作用是什么?它会显示,dropshadow,innershadow,gradient,stroke等等。它包含一组方法,所有这些方法都可以访问ApplyEffects的两个或三个属性。我想知道是否是“effect”对象不是线程安全的-因为所有线程都会同时访问该对象。是的,我尝试过在其中锁定不同的代码段。但是没有运气:(
    private static void GenerateIcons(Effects effect)
    {
        var dir     = new DirectoryInfo(HttpContext.Current.Server.MapPath(@"~\Icons\Original\"));
        var mappath = HttpContext.Current.Server.MapPath(@"~\Icons\");
        var ids     = GetAllEffectIds(effect.TinyUrlCode);
    
        var filesToProcess = dir
            .EnumerateFiles()
            .AsParallel()
            .Select(f => new { info = f, generated = File.Exists(mappath + @"Generated\" + ids + "-" + f.Name) })
            .ToList();
    
        Parallel.ForEach(filesToProcess.Where(f => !f.generated), file =>
        {
            new ApplyEffects(effect, file.info.Name, mappath).SaveIcon();
        });
    
        //Zip icons!
        ZipFiles(filesToProcess.Select(f => f.info), effect.TinyUrlCode, ids, effect.General.Prefix);
    }