"An item with the same key has already been added." when Parallel code
doylecnn opened this issue · 3 comments
Read and complete the full issue template
Do not randomly delete sections. They are here for a reason.
Do you want to request a feature or report a bug?
- Bug
- Feature
- Question
Did you test against the latest CI build?
- Yes
- No
If you answered No
, please test with the latest development build first.
Version of ClosedXML
0.95.4 and latest CI build (0.95.999.2311)
What is the current behavior?
get exception: An item with the same key has already been added. Key: 1
, An item with the same key has already been added. Key: 20
What is the expected behavior or new feature?
no exception
Is this a regression from the previous version?
No
Reproducibility
Code to reproduce problem:
public void Main()
{
var x = new ClosedXML.Excel.XLWorkbook();
var s = x.AddWorksheet();
var r = s.Row(1);
System.Threading.Tasks.Parallel.For(1, 1000000, i => {
var c1 = r.Cell(20);
c1.SetValue(i);
});
Console.ReadLine();
}
the stack trace from inner exception:
at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
at ClosedXML.Excel.XLCellsCollection.IncrementUsage(Dictionary`2 dictionary, Int32 key) in C:\projects\closedxml\ClosedXML\Excel\Cells\XLCellsCollection.cs:line 54
at ClosedXML.Excel.XLCellsCollection.Add(Int32 row, Int32 column, XLCell cell) in C:\projects\closedxml\ClosedXML\Excel\Cells\XLCellsCollection.cs:line 33
at ClosedXML.Excel.XLRangeBase.Cell(XLAddress& cellAddressInRange) in C:\projects\closedxml\ClosedXML\Excel\Ranges\XLRangeBase.cs:line 920
at ClosedXML.Excel.XLRangeBase.Cell(Int32 row, Int32 column) in C:\projects\closedxml\ClosedXML\Excel\Ranges\XLRangeBase.cs:line 848
at ClosedXML.Excel.XLRow.Cell(Int32 columnNumber) in C:\projects\closedxml\ClosedXML\Excel\Rows\XLRow.cs:line 144
lead me to:
- I attached a sample spreadsheet. (You can drag files on to this issue)
ClosedXML isn't thread-safe.
Suggestion: Add “ClosedXML is not thread-safe” to a “Limitations” section of the project readme.
Frequent answers section added to the README.