我正在创建一个Thread
执行一些长期流程并构建一个段落,我将在RichTextBox
.
通常,这只需通过以下方式完成:
Paragraph paragraph = new Paragraph();
paragraph.Inlines.Add(new TextBlock()
{
Text = "Hello i´m som text",
TextWrapping = TextWrapping.NoWrap
});
richtextbox.Document = new FlowDocument(paragraph);
(工作正常)
但我希望在一个线程中创建段落,然后添加到RichTextBox
类似:
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);
}
}
我的问题是这将返回错误:
调用线程无法访问此对象,因为不同的线程拥有它。在richtextbox.Document = new FlowDocument(paragraph);