---
title: "Adding text to RichTextBox #C / WPF"  
description: "Adding text to RichTextBox #C / WPF"  
author: "Anonymous User"  
published: 2013-12-17  
updated: 2013-12-17  
canonical: https://www.mindstick.com/forum/1789/adding-text-to-richtextbox-c-wpf  
category: "wpf"  
tags: ["wpf"]  
reading_time: 2 minutes  

---

# Adding text to RichTextBox #C / WPF

What I [am trying](https://answers.mindstick.com/qa/36834/which-two-programming-languages-should-i-master-in-if-i-am-trying-to-get-into-google-or-facebook) to achieve is to [add text](https://answers.mindstick.com/qa/96009/how-do-i-add-text-to-pdf-files-using-microsoft-edge) after every [operation](https://www.mindstick.com/forum/33917/how-to-callback-operation-using-delegate-in-c-sharp) to a RichTextBox. The [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic) is that these [operations](https://www.mindstick.com/blog/304985/how-does-devops-bridge-the-gap-between-development-and-operations-teams-like-git) take some time and [instead of](https://www.mindstick.com/articles/330/triggers-in-sql-server) viewing the appended text after every operation finishes, I [view](https://yourviews.mindstick.com/view/84701/layoffs-in-google-india-2023-view) them all at the end of the routine.

**Semi-Pseudo [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii):**

```
RichTextBox richTextBox = new RichTextBox()if (Operation1()){   richTextBox.AppendText("Operation1
finished");   if (Operation2())   {     
richTextBox.AppendText("Operation2 finished");      if
(Operation3())      {         richTextBox.AppendText("Operation3
finished");      }   }}
```

The problem is that I view the appended text of operation 1 & 2 after the operation 3 is finished.

I read somewhere that I need to use something called BackgroundWorker???

## Replies

### Reply by Pravesh Singh

**Hi Jeet,**

Using BackgroundWorker, you would just put the background work into DoWork, and the update into RunWorkerCompleted:

```
var bw1 = new BackgroundWorker();var bw2 = new BackgroundWorker();var bw3 = new BackgroundWorker();bw1.DoWork += (sender, args) => args.Result = Operation1();bw2.DoWork += (sender, args) => args.Result = Operation2();bw3.DoWork += (sender, args) => args.Result = Operation2();bw1.RunWorkerCompleted += (sender, args) => {    if ((bool)args.Result)    {       
richTextBox.AppendText("Operation1 ended\n");        bw2.RunWorkerAsync();    }};bw2.RunWorkerCompleted += (sender, args) => {    if ((bool)args.Result)    {       
richTextBox.AppendText("Operation2 ended\n");        bw3.RunWorkerAsync();    }};bw3.RunWorkerCompleted += (sender, args) => {    if ((bool)args.Result)    {       
richTextBox.AppendText("Operation3 ended\n");    }};bw1.RunWorkerAsync();
```

\

**\**

**You'll notice that this runs afoul of "DRY". You could always consider abstracting the tasks for each step using something like:**

***\***

```
var operations = new Func<bool>[] { Operation1,
Operation2, Operation3, };var workers = new BackgroundWorker[operations.Length];for (int i = 0; i < operations.Length; i++){    int locali = i;    // avoid
modified closure    var bw = new BackgroundWorker();    bw.DoWork += (sender, args) => args.Result
= operations[locali]();    bw.RunWorkerCompleted += (sender, args) =>    {        txt.Text =
string.Format("Operation{0} ended\n", locali+1);        if (locali <
operations.Length - 1)           
workers[locali + 1].RunWorkerAsync();    };    workers[locali] = bw;}workers[0].RunWorkerAsync();
```


---

Original Source: https://www.mindstick.com/forum/1789/adding-text-to-richtextbox-c-wpf

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
