My test code is:
- Code: Select all
public static void TestPdf(string fName)
{
try
{
using (var doc = new PdfDocument(fName))
{
var list = new List<(int page, PointF position, string text)>();
foreach (PdfPageBase page in doc.Pages)
{
var collection = page.FindAllText();
list.AddRange(collection.Finds.Select(find => (find.SearchPageIndex, find.Position, find.SearchText)));
}
}
}
catch (Exception ex)
{
Diag.LogException(ex);
}
}
public static void TestPdfParallel(params string[] files)
{
Parallel.ForEach(files, f => TestPdf(f));
}
if I call TestPdfParallel with bunch of files, I get something like this in exception log:
31.08.21 13:09:44 [275] 04/38 8 System.ArgumentException: An item with the same key has already been added.
at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
at sprᯊ.ᜁ()
at sprᯊ.ᜋ()
at sprᯌ.ᜀ(spr⭖ A_0, sprᝨ A_1)
at sprᯌ.ᜀ()
at sprᯌ..ctor(PdfPageBase A_0, sprᯆ A_1, Boolean A_2)
at sprᯌ..ctor(PdfPageBase A_0)
at spr᧱.ᜀ(PdfPageBase A_0)
at Util.PdfUtilSpire.TestPdf(String fName) in C:\APL\Mulema\Util\PdfUtilSpire.cs:line 228
31.08.21 13:09:44 [275] 04/41 9 System.ArgumentException: An item with the same key has already been added.
at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
at sprᯊ.ᜁ()
at sprᯊ.ᜋ()
at sprᯌ.ᜀ(spr⭖ A_0, sprᝨ A_1)
at sprᯌ.ᜀ()
at sprᯌ..ctor(PdfPageBase A_0, sprᯆ A_1, Boolean A_2)
at sprᯌ..ctor(PdfPageBase A_0)
at spr᧱.ᜀ(PdfPageBase A_0)
at Util.PdfUtilSpire.TestPdf(String fName) in C:\APL\Mulema\Util\PdfUtilSpire.cs:line 228
31.08.21 13:09:44 [275] 04/40 10 System.ArgumentException: An item with the same key has already been added.
at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
at sprᯊ.ᜁ()
at sprᯊ.ᜋ()
at sprᯌ.ᜀ(spr⭖ A_0, sprᝨ A_1)
at sprᯌ.ᜀ()
at sprᯌ..ctor(PdfPageBase A_0, sprᯆ A_1, Boolean A_2)
at sprᯌ..ctor(PdfPageBase A_0)
at spr᧱.ᜀ(PdfPageBase A_0)
at Util.PdfUtilSpire.TestPdf(String fName) in C:\APL\Mulema\Util\PdfUtilSpire.cs:line 228
Is it possible to redesign your algorithms to allow parallel calls of TestPdf in above example?
Sicerely,
Brane