ADDING TEXT TO RICHTEXTBOX #C / WPF

Jeet Verma

Total Post:110

Points:776
Posted by  Jeet Verma
WPF 
WPF
 2302  View(s)
Ratings:
Rate this:

What I am trying to achieve is to add text after every operation to a RichTextBox. The problem is that these operations take some time and instead of viewing the appended text after every operation finishes, I view them all at the end of the routine.

Semi-Pseudo code:

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???
  1. Pravesh Singh

    Post:412

    Points:2888
    Re: Adding text to RichTextBox #C / WPF

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

      Modified On Aug-11-2016 05:08:31 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!