Won't Fix
Last Updated: 23 Aug 2019 16:18 by ADMIN
ADMIN
Boby
Created on: 20 Mar 2018 07:02
Category: RichTextBox
Type: Bug Report
2
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;
dispatcher.BeginInvokeShutdown(DispatcherPriority.Normal);
Dispatcher.Run();

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);

            };
1 comment
ADMIN
Ivan Ivanov
Posted on: 23 Aug 2019 16:17

The exception occurs due to a leak of Dispatcher instances and their related infrastructure objects. When any DispatcherObject type is instantiated on a thread, a Dispatcher instance is associated with this thread. Even after the thread finishes successfully, its Dispatcher is not disposed. The described behavior is also reproducible with RadDocument, as it uses core WPF logic for some of its operations. For instance, using a Brush element on a thread automatically instantiates a Dispatcher object.

The exception is reproducible only in scenarios with heavy usage of new threads. If you plan incorporating such solutions, we suggest that you use the following workaround, also recommended by Microsoft:

Thread thread = new Thread(() =>
{
// Thread logic
Dispatcher.Current.BeginInvokeShutdown ();
});