Try this in xaml (i made demo):
<RichTextBox HorizontalAlignment="Left" Height="100" Margin="190,83,0,0" VerticalAlignment="Top" Width="100">
<RichTextBox.Resources>
<Style TargetType="{x:Type Paragraph}">
<Setter Property="Margin" Value="0"/>
</Style>
</RichTextBox.Resources>
<FlowDocument>
<Paragraph>
<Run Text="RichTextBox"/>
</Paragraph>
</FlowDocument>
</RichTextBox>
And in code:
//this can be added in your invoke method
mainWindow.Dispatcher.Invoke(new Action(() => DoSomething));
private void DoSomething(){
string myText = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd).Text;
var resultString = Regex.Replace(myText, @"( |\r?\n)\1+", "$1");
MemoryStream stream = new MemoryStream(ASCIIEncoding.Default.GetBytes(resultString));
richTextBox.SelectAll();
richTextBox.Selection.Load(stream, DataFormats.Text);
}