C# WPF Dispatcher in richtextbox.Document.Blocks
C# WPF Dispatcher in richtextbox.Document.Blocks
all working ok when Im using in Thread:
richtextbox1.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => { this.richtextbox1.AppendText(text); }));
but I get error "The calling thread cannot access this object because a different thread owns it." when Im using in Thread:
richtextbox1.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => { this.richtextbox1.Document.Blocks.Add(paragraph); }));
Im also get this error if I try with richtextbox1.Document = some_FlowDocument
Can't find way how to resolve this in WPF
paragraph
What's the difference between "using in Thread" and "using in Thread", i.e. when does it work and when does it not?
– mm8
Jun 25 at 8:40
you can see in code
– ワタルシロ
Jun 25 at 11:25
2 Answers
2
Probebly paragraph
is a UI element too.
paragraph
Try the following and let me know if it works:
string txt=paragraph;
richtextbox1.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => { this.richtextbox1.Document.Blocks.Add(txt); }));
paragraph is not UI element:
Paragraph paragraph = new Paragraph(); paragraph.Inlines.Add(new Bold(new Run("some_text")) { Foreground = Brushes.Red });
– ワタルシロ
Jun 24 at 13:39
Paragraph paragraph = new Paragraph(); paragraph.Inlines.Add(new Bold(new Run("some_text")) { Foreground = Brushes.Red });
need make FlowDocument and Paragraph in main thread and invoke
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Well what is
paragraph
? If it's a UI element that you created in a different thread, that may well be the reason. As an aside, you may well find that using async/await is a simpler way of going about all of this.– Daisy Shipton
Jun 23 at 20:07