An ArgumentOutOfRangeException can occur when there are multiple
threads doing Document.Save at the same time. The stack trace is always the same.
The attached C# project
runs Open/Save on the same document 100 times. It does that with 1 thread, then 2 threads, then 4 threads. A new Document object is created for each Open/Save, objects are not reused or shared. Exceptions are logged to the log.txt file. See attached log.txt for output from a sample run on a quad core. Notice that there are no exceptions when only 1 thread is used, there are several exceptions with 2 threads, and there are many exceptions with 4 threads. The number of exceptions changes in each run, so it is clearly a race condition. I’ve only tested with a quad-core processor. I imagine the problem is
less repeatable with dual-core and possibly not repeatable with
Although this sample project does Document.Save to a MemoryStream, the same problem occurs when writing to files. This sample project uses the same document each time, because it makes for a simpler test case and is more likely to reproduce the error, but I originally discovered the problem when processing a large queue of unique documents.