1

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

greg
  • 1,289
  • 2
  • 14
  • 33
Lynspitti
  • 26
  • 4
  • http://stackoverflow.com/questions/2728896/the-calling-thread-cannot-access-this-object-because-a-different-thread-owns-it/2729040#2729040 – Terry Jun 14 '13 at 09:43
  • sorry but cant see the connection.... – Lynspitti Jun 14 '13 at 10:40
  • The error line is pretty obvious, you're trying to change the content of a UI element through a thread that is not the current UI Thread. So you have to get the thread that has access on let that one update your control. – Terry Jun 14 '13 at 11:18
  • and if i do that, will my code take 1 minute and 30 seconds to process.... and the idea off a sub thread is gone, cause i nearly always process data to the paragraph that is used be UI thread. I thought what the idea of a sub thread was to create the longtime process and return the result to the UI, but correct me if i´m wrong. – Lynspitti Jun 14 '13 at 11:31
  • The only way to get your UI updates is to get the thread that handles the UI, so performance wise, there's no other option. You can always use a different thread to do all the work on and then call the UI thread, that's still a different case. – Terry Jun 14 '13 at 11:51
  • The Paragraph instance was created in a different thread and you trying to add it into richtextbox that was created in main UI thread. – Jawahar Jun 14 '13 at 11:55
  • yes cause it takes to long to let the UI Thread create the Paragraph it self.. – Lynspitti Jun 14 '13 at 12:32
  • Thanks Djerry, but still my main problem is, that the point in this thread is to create the Paragraph, cause it is a lot to process 3000 lines in a Paragraph, and takes way to long time to process as UI thread.. But maybe your right i will try to find a way and let the UI create the object. – Lynspitti Jun 14 '13 at 12:42

1 Answers1

0

I already shared the solution to this problem as an answer to the similar question, please refer to the SO answer and my blog post. In terms of performance and lack of UI blocking if should fit your needs. If you have found another way of solving the problem since 2013, can you also share?

Community
  • 1
  • 1
too
  • 3,009
  • 4
  • 37
  • 51