This is a migrated thread and some comments may be shown as answers.

System.ComponentModel.Win32Exception

15 Answers 148 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Serhat
Top achievements
Rank 1
Serhat asked on 28 Apr 2014, 08:31 AM
We are getting System.ComponentModel.Win32Exception error while  converting XAML to PDF at WCF WebSevice. Interestingly WCF Service sometimes getting this error, sometimes not. Also this code working inside a Thread otherwise it gives a STA Thread Exception . 

Telerik dll version : 2013.1.527.45  AND .Net 4.5

Code and detailed error is shown below. Please immediate help.

byte[] XAMDataToBeConverted;

 var xamlProvider = new Telerik.Windows.Documents.FormatProviders.Xaml.XamlFormatProvider();
 radDocument = xamlProvider.Import(XAMDataToBeConverted); //Getting Error on this line
 xamlProvider = null;

The Error detail is 

'The invocation of the constructor on type 'Telerik.Windows.Documents.Model.RadDocument' that matches the specified binding constraints threw an exception.' Line number '1' and line position '569'. System.Windows.Markup.XamlParseException Seneka.Ebdys.Server.Services.HelperClasses.RadRichTextBoxHelperServer CreatePdfSimple "System.Windows.Markup.XamlParseException: 'The invocation of the constructor on type 'Telerik.Windows.Documents.Model.RadDocument' that matches the specified binding constraints threw an exception.' Line number '1' and line position '569'. ---> System.ComponentModel.Win32Exception: The operation completed successfully
   at MS.Win32.HwndWrapper..ctor(Int32 classStyle, Int32 style, Int32 exStyle, Int32 x, Int32 y, Int32 width, Int32 height, String name, IntPtr parent, HwndWrapperHook[] hooks)
   at System.Windows.Threading.Dispatcher..ctor()
   at Telerik.Windows.Documents.Model.MailMergeDataSource..ctor()
   at Telerik.Windows.Documents.Model.RadDocument..ctor()
   at System.Xaml.Schema.XamlTypeInvoker.CreateInstance(Object[] arguments)
   at MS.Internal.Xaml.Runtime.ClrObjectRuntime.CreateInstance(XamlType xamlType, Object[] args)
   --- End of inner exception stack trace ---
   at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
   at System.Windows.Markup.XamlReader.Load(XamlReader xamlReader, ParserContext parserContext)
   at System.Windows.Markup.XamlReader.Load(XmlReader reader, ParserContext parserContext, XamlParseMode parseMode)
   at Telerik.Windows.Documents.FormatProviders.Xaml.XamlFormatProvider.Import(Stream input)
   at Telerik.Windows.Documents.FormatProviders.DocumentFormatProviderBase.Import(Byte[] input)
   at Seneka.Ebdys.Server.Services.HelperClasses.RadRichTextBoxHelperServer.CreatePdfSimple()"







15 Answers, 1 is accepted

Sort by
0
Boby
Telerik team
answered on 30 Apr 2014, 08:27 AM
Hi Serhat,
The version you are using has a limitation implying that RadDocument should be used only in STA thread. This most probably leads you to an issue in .NET framework, related to a large number of threads created. You have two options:
- Upgrade to the latest version. Now RadDocument can be used in normal (MTA) background thread, moreover some concurrency issues are overcome (import/export is now thread-save).
- Try the workaround suggested in the mentioned Connect issue:

You can work around this bug by adding the following code to your thread proc:
Dispatcher dispatcher = Dispatcher.CurrentDispatcher;
dispatcher.BeginInvokeShutdown(DispatcherPriority.Normal);
Dispatcher.Run();
This asks the dispatcher associated with the thread to shut down right away.



I hope this helps.

Regards,
Boby
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
Muhammad
Top achievements
Rank 1
answered on 13 Jun 2017, 03:09 AM

We are getting the exception as below get this exception, This code is importing an valid XML from data base to RADdocument. This code is working fine on our local systems and never produce any issue but in production environment Sometimes it throws below Exception.Moreover we are not using the STA Thread.

we are using .Net 4.0 and version of Telerik.Windows.Documents.DLL : 2014.1.331.40

Below is the code which is producing Exception.

RadDocument radDocument = null;
 try { tMPL_CNFGInfo = instance.GetStationeryConfigDetails(requestObject).ResultSet;
      XamlFormatProvider xamlFormatProvider = new XamlFormatProvider();                     
      radDocument = xamlFormatProvider.Import(tMPL_CNFGInfo.TEMPLATECONFIGBODY);}            
 catch (System.Exception ex)

 

'The invocation of the constructor on type 'Telerik.Windows.Documents.Model.RadDocument' that matches the specified binding constraints threw an exception.' Line number '1' and line position '569'.

Type: System.Windows.Markup.XamlParseException:
Source:
PresentationFramework
Data:
Stack Trace:    at System.Windows.Markup.XamlReader.RewrapException(Exception e, IXamlLineInfo lineInfo, Uri baseUri)
   at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
   at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, Boolean skipJournaledProperties, Uri baseUri)
   at System.Windows.Markup.XamlReader.Load(XamlReader xamlReader, ParserContext parserContext)
   at System.Windows.Markup.XamlReader.Load(XmlReader reader, ParserContext parserContext, XamlParseMode parseMode)
   at System.Windows.Markup.XamlReader.Load(XmlReader reader)
   at System.Windows.Markup.XamlReader.Parse(String xamlText)
   at Telerik.Windows.Documents.FormatProviders.Xaml.XamlFormatProvider.Import(String input)
   at NFS.Business.Services.StationaryService.evaluateDocument(RequestObject`1 requestObject)

===INNER EXCEPTION===

The operation completed successfully

Type: System.ComponentModel.Win32Exception:
Source:WindowsBase
Data:
Stack Trace:    at MS.Win32.UnsafeNativeMethods.RegisterClassEx(WNDCLASSEX_D wc_d)
   at MS.Win32.HwndWrapper..ctor(Int32 classStyle, Int32 style, Int32 exStyle, Int32 x, Int32 y, Int32 width, Int32 height, String name, IntPtr parent, HwndWrapperHook[] hooks)
   at System.Windows.Threading.Dispatcher..ctor()
   at System.Windows.Threading.Dispatcher.get_CurrentDispatcher()
   at Telerik.Windows.Documents.Model.MailMergeDataSource..ctor()
   at Telerik.Windows.Documents.Model.RadDocument..ctor()
   at System.Xaml.Schema.XamlTypeInvoker.DefaultCtorXamlActivator.InvokeDelegate(Action`1 action, Object argument)
   at System.Xaml.Schema.XamlTypeInvoker.DefaultCtorXamlActivator.CallCtorDelegate(XamlTypeInvoker type)
   at System.Xaml.Schema.XamlTypeInvoker.DefaultCtorXamlActivator.CreateInstance(XamlTypeInvoker type)
   at System.Xaml.Schema.XamlTypeInvoker.CreateInstance(Object[] arguments)
   at MS.Internal.Xaml.Runtime.ClrObjectRuntime.CreateInstanceWithCtor(XamlType xamlType, Object[] args)
   at MS.Internal.Xaml.Runtime.ClrObjectRuntime.CreateInstance(XamlType xamlType, Object[] args)

0
Boby
Telerik team
answered on 13 Jun 2017, 05:46 AM
Hello Muhammad,

The problem is exactly the same. MailMergeDataSource is DependencyObject, which causes this problem as it starts dispatcher which is not properly stopped.

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.


Regards,
Boby
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Muhammad
Top achievements
Rank 1
answered on 12 Jun 2018, 09:05 AM

Hello, The above issue was resolved when we remove the parallel loop that Call the above Mentioned code, but now as the calling frequency increased this issue above issue reported by me is happening again. The code which i mentioned in my above post is in a wcf Service so as you suggest to put below codeis not working for us as it wcf channel closed by the below suggested code and exception occurred. Please help accordingly .

Dispatcher dispatcher = Dispatcher.CurrentDispatcher;
dispatcher.BeginInvokeShutdown(DispatcherPriority.Normal);
Dispatcher.Run();

0
Boby
Telerik team
answered on 13 Jun 2018, 08:02 AM
Hello Muhammad,

The problem we know about is related to starting multiple threads, which internally create a Dispatcher object which then cannot be shut down automatically. There are two possible workarounds:

- In each thread, manually invoke the BeginInvokeShutdown (as in the provided workaround). As I understand, the straightforward scenario would be to start a new thread, do some document processing there, and invoke the BeginInvokeShutdown also there (and this should not affect the "main" WCF thread; but I may be missing come context here.

- Implement some kind of thread pooling. This way, the Dispatchers are not shut down, but their number is limited to the number of the threads in the pool, thus avoiding the exception. You can check the public item for a description on how to implement such approach: RichTextBox: Win32Exception is thrown when RadDocuments are processed in background threads.

Regards,
Boby
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Muhammad
Top achievements
Rank 1
answered on 13 Jun 2018, 08:20 AM

Hello,

Thanks for your support, Could you please guide me regarding its solution by updating Telerik Controls. i mean if i update the controls it will fix that issue or not, if the update can fix it then which version of controls should be used. Please also guide me in case update of .Net Framework is required or not.

Actually i will prefer the solution by upgrade rather than code change.

Regards,

Usman Shahid.

0
Boby
Telerik team
answered on 18 Jun 2018, 07:02 AM
Hi Muhammad,

As a possible fix for the item the team will try to remove the DependencyObject-s from the document model, but this is currently not scheduled for a specific release, and it may not be possible at all. For now, it would be best to use any of the provided workarounds.

Regards,
Boby
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Muhammad
Top achievements
Rank 1
answered on 21 Jun 2018, 07:17 AM

Hello Boby,

Actually we are unable to use the work around because of some limitations at our end, point of concern is that this issue occurs at WCF Service side and our wcf Service have Instance Mode as per session but as we are not creating session so it should behave as per call, now after every call this wcf thread is being auto closed and in one thread rad document is used only once so why we still need to create a new thread and then close it by dispatcher ?. Could you please guide any other workaround other than previously shared workarounds. e.g. if we can use async calling and discard relevant async resource to free dependency object?

0
Boby
Telerik team
answered on 25 Jun 2018, 01:45 PM
Hi Muhammad,

The problem originally discussed in this thread is related to starting multiple threads without property shutting down a Dispatcher object automatically created in them. The provided workarounds work by ensuring that the dispatcher is shutdown at the end of the thread life time, or by limiting the number of used thread by using thread pooling. If the workarounds are not sufficient, or the scenario is different, I would advice on opening a support ticket, where you can provide sample code/project demonstrating the problem and why the workaround doesn't work in such case.

Regards,
Boby
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Muhammad
Top achievements
Rank 1
answered on 18 Oct 2018, 12:00 PM

Hi Boby,

We are still facing the above discussed issue as creating a new thread then processing RadDocument and then close that thread is not possible in our case because of some limitations.

I have another idea that if we use using() to create RadDocument object then is it can solve the problem and if it cannot solve then please elaborate for my understanding. Secondly i have checked the code of RadDocument and XMLFormatProvider i did not found any dispatched in it which is initiated and not closed, could you please guide me for this.

Regards,

Usman Shahid.

0
Boby
Telerik team
answered on 22 Oct 2018, 06:57 AM
Hi Muhammad,

The dispatchers are created and started internally because the document model contains objects that inherit from DependencyObject. This dispatchers are not shut down automatically by WPF (this is by design, according to the WPF team), and when too many threads are started, some kind of system resource is exhausted. 

There are two possible workarounds - to shut down the dispatchers explicitly, or to create less threads, by using thread pooling. Both approaches are described in more details in my answer.

Regards,
Boby
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Muhammad
Top achievements
Rank 1
answered on 06 Nov 2018, 09:48 AM

Hi Boby,

I have found the fixation below are the details. Please consider them for future support.

As we are using RadDocument at Server Side (WCF Service), we are using per call instance method, so every wcf service call initiate a thread and then close it although WCF service call thread pooling is also used by IIS. So as per solution provided by telerik it should not happen in that case because pooling and thread closing is already there.

By somehow issue was happening, may be it is because of Garbage collection as it was not good enough in .Net 4.0.

We updated our production Env. to 4.6.1 and it resolves the issue.

Please share your views on it.

Regards,

M. Usman Shahid.

0
Boby
Telerik team
answered on 08 Nov 2018, 03:28 PM
Hello Muhammad,

I am glad that you managed to find a solution! Also thank you for taking time to share it back here.

Regards,
Boby
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Alex
Top achievements
Rank 1
answered on 29 Aug 2019, 01:43 PM

Hi,

We are using DLL version 2018.1.220.40 and even with the workaround this don't work properly.
We have framework 4.6.1 and its a WCF project that generates a PDF from HTML.

Now we are getting:

System.InvalidOperationException: Cannot perform requested operation because the Dispatcher shut down.
   at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
   at Wallet.DocumentGenerator.DocumentGenerator.<>c__DisplayClass2_1.<InitGeneratePDFFromHTML>b__0()
   at System.Threading.Tasks.Task.Execute()

We tried to implement a workaround like, try to generate the document a max of 10 times and after that throw exception, but even with this the error above occurs many times.

Is the main bug still not fixed? Do you suggest other workaround?

Regards,
Alex Paula

0
Boby
Telerik team
answered on 30 Aug 2019, 12:02 PM

Hello Alex,

I am already having the discussion with you and and your colleagues in an internal support ticket, where the problem is indicated to be related with Telerik Document Processing (this is also confirmed by the stack traces), but this forum is related to RadRichTextBox control.

Anyway, it seems that the problem could be similar here and you have to initiate dispatcher shutdown only for the threads used for document processing, and not of the main service thread, which dispatcher is probably used to keep the service alive.

Regards,
Boby
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
RichTextBox
Asked by
Serhat
Top achievements
Rank 1
Answers by
Boby
Telerik team
Muhammad
Top achievements
Rank 1
Alex
Top achievements
Rank 1
Share this question
or