I´m creating a Thread
to do some long run process and building a Paragraph that I shall show in a RichTextBox
.
Normally this is simply done by:
Paragraph paragraph = new Paragraph();
paragraph.Inlines.Add(new TextBlock()
{
Text = "Hello i´m som text",
TextWrapping = TextWrapping.NoWrap
});
richtextbox.Document = new FlowDocument(paragraph);
(Works Fine)
But I want the Paragraph to be created in a thread and then added to the RichTextBox
like:
Thread t = new Thread(new ThreadStart(CreateText));
t.Start();
private void CreateText()
{
Paragraph paragraph = new Paragraph();
paragraph.Inlines.Add(new TextBlock()
{
Text = "Hello i´m som text",
TextWrapping = TextWrapping.NoWrap
});
Main.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(() =>
{
richtextbox.Document = new FlowDocument(paragraph);
}
}
My problem is that this will return Error:
The calling thread cannot access this object because a different thread owns it. on richtextbox.Document = new FlowDocument(paragraph);