简体   繁体   中英

STA-Thread - Exception

I've got a thread having a print-job which creates a View and it's ViewModel and initializes them.

Without the thread:

The ViewModel has some bindings and is correctly initialized after giving it to the View.

//MyView : UserControl
//Constructor
DataContext = vm;
InitializeComponent();

With the thread:

During InitializeComponent() an exception is thrown.

I already checked answers on here and Google'd for it and found only the "usual" error - not running the thread in STA. This does not seem to apply for my code.

//Join thread if valid
//set to null
PrintThread = new Thread(new ParameterizedThreadStart(ExecutePrintJob));
PrintThread.SetApartmentState(ApartmentState.STA);
PrintThread.Start(); //Takes parameter but unused at the moment


//Translated it
Exception thrown: 'System.Windows.Markup.XamlParseException' in PresentationFramework.dll
Additional information: Line number "25" und Line position "43" from "An exception occured during the initialization of "System.Windows.Controls.Label.".


<Label  Content="TextBox --->"
        Grid.Column="0" Grid.Row="0"
        VerticalAlignment="Center" HorizontalAlignment="Center" />

What could possibly throw an System.Windows.Markup.XamlParseException when using this inside another thread (not a task) ? I'm not sure how I can fix this issue, already asked a few people.

Well looking at the exception the component you're using for printing would use a "System.Windows.Controls.Label" that is a UI component, therefore it should be instantiated on the main UI thread. I would try marshalling the call on that thread. I run in a maybe similar issue (even if in a different context) and I posted my solution here, I hope it helps:

Why WPF button's command's CanExecute method is not called even if I call CommandManager.InvalidateRequerySuggested()?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM