Multi-threading issue with GetDisplayStyle

The following code will throw an error sometimes when GetDisplayStyle is run muti-threaded.

The file is attached and the error stack is below.

Thank You,
-Andy

    [Test]
    public Task foo66()
    {
        var filename = @"C:\Data\AsposeCellTest.xlsx";
        var options = new ParallelOptions() { MaxDegreeOfParallelism = 1000 };

        var workbook = new Workbook(filename);
        var cells = workbook.Worksheets.First().Cells;
        var cellsList = new List<Aspose.Cells.Cell>();
        for(int i = 0; i < cells.Rows.Count; i++)
        {
            for(int j = 0; j < cells.Columns.Count; j++)
            {
                cellsList.Add(cells[i, j]);
            }
        }

        Parallel.ForEach(cellsList, options, cell =>
        {
            var displayStyle = cell.GetDisplayStyle(true);
        });
        
        return Task.CompletedTask;
    }

AsposeCellTest.zip (10.3 KB)


System.AggregateException : One or more errors occurred. (Object reference not set to an instance of an object.)
  ----> System.NullReferenceException : Object reference not set to an instance of an object.
   at System.Threading.Tasks.TaskReplicator.Run[TState](ReplicatableUserAction`1 action, ParallelOptions options, Boolean stopOnFirstFailure)
   at System.Threading.Tasks.Parallel.ForWorker[TLocal,TInt](TInt fromInclusive, TInt toExclusive, ParallelOptions parallelOptions, Action`1 body, Action`2 bodyWithState, Func`4 bodyWithLocal, Func`1 localInit, Action`1 localFinally)
--- End of stack trace from previous location ---
   at System.Threading.Tasks.Parallel.ForWorker[TLocal,TInt](TInt fromInclusive, TInt toExclusive, ParallelOptions parallelOptions, Action`1 body, Action`2 bodyWithState, Func`4 bodyWithLocal, Func`1 localInit, Action`1 localFinally)
   at System.Threading.Tasks.Parallel.ForEachWorker[TSource,TLocal](IEnumerable`1 source, ParallelOptions parallelOptions, Action`1 body, Action`2 bodyWithState, Action`3 bodyWithStateAndIndex, Func`4 bodyWithStateAndLocal, Func`5 bodyWithEverything, Func`1 localInit, Action`1 localFinally)
   at System.Threading.Tasks.Parallel.ForEach[TSource](IEnumerable`1 source, ParallelOptions parallelOptions, Action`1 body)
   at QueBIT.ReportWORQ.UnitTestsV5.Sandbox.ADW.foo66() in C:\Users\AndyWeiss\repos\ReportWORQ\QueBIT.ReportWORQ.UnitTestsV5\Sandbox\ADW.cs:line 95
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
--NullReferenceException
   at \u0005\u0019\u001a.\u0002(Row \u0002, Boolean \u0008, Boolean \u0005)
   at \u000f\u0019\u0003.\u0005\u0019\u001a\u0003\u0016\u0002(Int32 \u0002, Boolean \u0008, Boolean \u0005, Boolean \u0006)
   at Aspose.Cells.RowCollection.\u0002(Int32 \u0002, Boolean \u0008, Boolean \u0005, Boolean \u0006)
   at \u0008\u001a\u001a.\u000e\u0002\u001a\u0003\u0016\u0008(Int32 \u0002)
   at Aspose.Cells.Cells.\u0002(Style \u0002, Int32 \u0008, Int32 \u0005, Int32 \u0006, Int32 \u0003, Boolean \u000e, \u000e\u0002\u001a \u000f)
   at Aspose.Cells.Cell.\u0002(\u000f\u0015\u001a \u0002)
   at Aspose.Cells.Cell.\u0002(Boolean \u0002, StyleModifyFlag \u0008, Int32 \u0005)
   at Aspose.Cells.Cell.GetDisplayStyle(Boolean includeMergedBorders)
   at QueBIT.ReportWORQ.UnitTestsV5.Sandbox.ADW.<>c.<foo66>b__1_0(Cell cell) in C:\Users\AndyWeiss\repos\ReportWORQ\QueBIT.ReportWORQ.UnitTestsV5\Sandbox\ADW.cs:line 97
   at System.Threading.Tasks.Parallel.<>c__DisplayClass19_0`2.<ForWorker>b__1(RangeWorker& currentWorker, Int64 timeout, Boolean& replicationDelegateYieldedBeforeCompletion)
--- End of stack trace from previous location ---
   at System.Threading.Tasks.Parallel.<>c__DisplayClass19_0`2.<ForWorker>b__1(RangeWorker& currentWorker, Int64 timeout, Boolean& replicationDelegateYieldedBeforeCompletion)
   at System.Threading.Tasks.TaskReplicator.Replica.Execute()


@weissa

Thank you for providing the resources to reproduce the issue, we will evaluate it soon and give feedback.

At the same time, would you please try the Cells.MultiThreadReading property by setting it as true before the multi-threads processing to check whether it can solve the issue?

@weissa

By our test, as we suggested, adding one line of code:

            ...
            cells.MultiThreadReading = true;
            var cellsList = ...

can make the process work fine. Hope it can solve your issue too at your end.

@johnson.shi,

This works great. Thank you!

-Andy

@weissa
Thank you for your feedback. I’m glad your issue has been resolved. If you have any questions, please feel free to contact us at any time.