Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.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# Parallel.ForEach处理和添加到ConcurrentDictionary中_C#_.net_Multithreading_Concurrency_Parallel Processing - Fatal编程技术网

C# Parallel.ForEach处理和添加到ConcurrentDictionary中

C# Parallel.ForEach处理和添加到ConcurrentDictionary中,c#,.net,multithreading,concurrency,parallel-processing,C#,.net,Multithreading,Concurrency,Parallel Processing,我想把字节转换成pdf格式。因为我想加快进程,使用并行/多线程功能 问题是当MaxDegreeOfParallelism大于1时,它会挂起或中断。我想内存已经被破坏了,有人能给点建议吗 public ConcurrentDictionary<Int64, Byte[]> letterContentDictionary = new ConcurrentDictionary<Int64, Byte[]>(); private void ProcessFilesInParal

我想把字节转换成pdf格式。因为我想加快进程,使用并行/多线程功能

问题是当MaxDegreeOfParallelism大于1时,它会挂起或中断。我想内存已经被破坏了,有人能给点建议吗

public ConcurrentDictionary<Int64, Byte[]> letterContentDictionary = new ConcurrentDictionary<Int64, Byte[]>();

private void ProcessFilesInParallel()
{
var files = _oldDbContext.LetterData.Where(x => x.ByteContent != null).Select(y => y).Take(100);
Parallel.ForEach(files, new ParallelOptions { MaxDegreeOfParallelism = 4 }, file =>
{
    byte[] byteCompleteLetterPdf = null;
    byteCompleteLetterPdf = ConvertToPdfWithAspose(file.ByteContent);
    letterContentDictionary.TryAdd(file.LetterId, byteCompleteLetterPdf);
});
}

尝试先在数组中缓冲DbContext结果,如下所示:

var files = _oldDbContext.LetterData.Where(x => x.ByteContent != null).Select(y => y).Take(100).ToArray();

当并行循环中断时,您看到了什么错误?您确定方法ConvertToPdfWithAspose是线程安全的吗?