20 Mar 2018 07:04
RichTextBox: Win32Exception is thrown when RadDocuments are processed in background threads
"Win32Exception (0x80004005): Not enough storage is available to process this command" is thrown when multiple RadDocument instances are created in background threads.
This is due to the fact that RadDocument contains MailMergeDataSource object, which is DependencyObject. When multiple dependency objects are created in different threads, they are not property freed by thread's Dispatcher.
Workaround 1: Microsoft provided workaround in a bug report here:
https://connect.microsoft.com/VisualStudio/feedback/details/620588/system-componentmodel-win32exception-0x80004005-not-enough-storage-is-available-to-process-this-command , namely:
Put the following code:
Dispatcher dispatcher = Dispatcher.CurrentDispatcher;
anywhere in the background thread processing the RadDocument.
Workaround 2: Use pooling of the used threads. The problem is that Task Parallel Library (which has built-in pooling) does not use STA threads by default, but this can be achieved by using custom TaskScheduler (attached, the code is get from here: https://code.msdn.microsoft.com/ParExtSamples ):
var parallelOptions = new ParallelOptions()
TaskScheduler = new StaTaskScheduler(Environment.ProcessorCount)
Parallel.For(0, 10000, parallelOptions, cc =>
var document = new DocxFormatProvider().Import(data);
byte pdfBytes = new PdfFormatProvider().Export(document);