Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic Create Controls in STA Thread (Read 1680 times)
Ivan K.
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 9
Joined: Jun 30th, 2021
Create Controls in STA Thread
Aug 31st, 2023 at 6:10am
Print Post  
Hello. I would like to create a diagram in a separate thread and use SvgExporter to export it into SVG. For that purpose, I create a separate thread and call thread.SetApartmentState(ApartmentState.STA); on it. But I get the "The calling thread cannot access this object because a different thread owns it." exception like this:

Code
Select All
System.TypeInitializationException: The type initializer for 'MindFusion.Diagramming.Wpf.Diagram' threw an exception.
 ---> System.TypeInitializationException: The type initializer for 'MindFusion.Diagramming.Wpf.GraphicsUnit' threw an exception.
 ---> System.InvalidOperationException: The calling thread cannot access this object because a different thread owns it.
   at System.Windows.Threading.Dispatcher.<VerifyAccess>g__ThrowVerifyAccess|7_0()
   at System.Windows.Application.get_MainWindow()
   at MindFusion.Utilities.()
   at MindFusion.Diagramming.Wpf.GraphicsUnit..cctor()
   --- End of inner exception stack trace ---
   at MindFusion.Diagramming.Wpf.Diagram..cctor()
   --- End of inner exception stack trace ---
   at MindFusion.Diagramming.Wpf.Diagram..ctor()
   at MindFusionStaTest.MainWindow.CreateDiagram() in D:\work\MindFusionStaTest\MainWindow.xaml.cs:line 38
   at MindFusionStaTest.MainWindow.<>c__DisplayClass2_0.<STAButton_OnClick>b__0() in D:\work\MindFusionStaTest\MainWindow.xaml.cs:line 28
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
 



I prepared the sample application (attached) that allows to reproduce the issue.

Could you please take a look, what I'm doing wrong? Maybe I should use another approach of creating MindFusion diagram in another thread. Thanks!
  

MindFusionStaTest.zip ( 3 KB | 121 Downloads )
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Online


tech.support

Posts: 3440
Joined: Oct 19th, 2005
Re: Create Controls in STA Thread
Reply #1 - Sep 1st, 2023 at 8:52am
Print Post  
Hi,

We'd need to remove or Freeze() a bunch of static property defaults for this to work. We'll have it in mind for v4 release. You could export from a separate process meanwhile.

Regards,
Slavcho
Mindfusion
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Online


tech.support

Posts: 3440
Joined: Oct 19th, 2005
Re: Create Controls in STA Thread
Reply #2 - Sep 25th, 2023 at 5:22am
Print Post  
This build should work from STA thread -
https://mindfusion.eu/_beta/wpfdiag4.zip

You'll need to freeze the bitmap too before passing it to UI thread:
Code
Select All
var thread = new Thread(() =>
{
	image = CreateDiagram();
	image.Freeze();
});
...
Image.Source = image;
 



Regards,
Slavcho
Mindfusion
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint