-2

I tried to display the string into the richtextbox, but always got the same or duplicate string after the second time it was run like in the example of displaying a second string.

Example of displaying the first string:

[21:55:07] - [#] Starting scanning, please wait. . .

[21:55:09] - [√] Finished.

Example of displaying a second string:

[21:55:07] - [#] Starting scanning, please wait. . .

[21:55:09] - [√] Finished.

[21:55:07] - [#] Starting scanning, please wait. . .

[21:55:09] - [√] Finished.

    private void Display(string text, bool newLine = true)
    {
        DisplayTextBox.AppendText(text);
        if (newLine)
        {
            DisplayTextBox.AppendText(Environment.NewLine);
            DisplayTextBox.ScrollToCaret();
        }
    }

        private void startScan()
        {
            Display("...........................................................................................", true);
            Display("[ " + DateTime.Now.ToLongTimeString() + " ] - [ # ] Starting scanning, please wait . . . . . .", true);
            Display("...........................................................................................", true);
            Display(Environment.NewLine, true);
            Thread.Sleep(100);
            this.DirButton.Enabled = false;
            this.ScanButton.Enabled = false;
            scanned = 0;
            skipped = 0;
            worker.WorkerReportsProgress = true;
            worker.DoWork += Scan;
            worker.ProgressChanged += this.ProgressChanged;
            worker.RunWorkerCompleted += this.RunWorkerCompleted;
            worker.RunWorkerAsync();
        }

        private static void Scan(object sender, DoWorkEventArgs e)
        {
            startScan(e.Argument.ToString());
        }

        private void ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            Display(e.UserState.ToString(), true);   
        }

        private void RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                this.Display("[ " + DateTime.Now.ToLongTimeString() + " ] - [ ! ] Exception: " + e.Error.ToString(), true);
            }
            if (!e.Cancelled)
            {
                string text = "[ " + DateTime.Now.ToLongTimeString() + " ] - Scanned [ " + scanned + " ] files ";
                string text2 = "[ " + DateTime.Now.ToLongTimeString() + " ] - Skipped [ " + skipped + " ] files ";
                Display(Environment.NewLine,true);
                Display("--------------------------------------------------------------------------------------", true);
                Display("[ " + DateTime.Now.ToLongTimeString() + " ] - [ √ ] Finished.", true);
                Display("--------------------------------------------------------------------------------------", true);
                Thread.Sleep(100);
                if (Form1.skipped > 0)
                {
                    Display(text2,true);
                }

                else if (Form1.skipped == 0)
                {
                    Display(text2,true);
                }

                this.Display(text,true);
                Display("--------------------------------------------------------------------------------------", true);
                Thread.Sleep(100);
                DirButton.Enabled = true;
                ScanButton.Enabled = true;
                return;
            }
            Display("[ " + DateTime.Now.ToLongTimeString() + " ] - [ x ] Scanning cancelled", true);
            DirButton.Enabled = true;
            ScanButton.Enabled = true;
            return;
        }

    private static int scanned = 0;
    private static int skipped = 0;
    private static BackgroundWorker worker = new BackgroundWorker();

how do I prevent it from displaying duplicate strings like in the example second string?

Nurohman AR
  • 75
  • 1
  • 11

1 Answers1

1

I ~think~ you simply want the RichTextBox to start fresh when the scan is started again?

If so, use the Clear() method at the beginning of startScan():

private void startScan()
{
    DisplayTextBox.Clear();

    // ... rest of your existing code ...

}

---------- EDIT ----------

You are wiring up the events of the backgroundworker, each time the scan is performed. This should only be done ONCE. A good place would be the Load() event of the form.

These are the lines that need to be moved, and only called once:

worker.DoWork += Scan;
worker.ProgressChanged += this.ProgressChanged;
worker.RunWorkerCompleted += this.RunWorkerCompleted;
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
  • I have tried adding              if (DisplayTextBox.Text ! = null)              {                  DisplayTextBox.Clear ();              } but still there is no change, the second time the scan button is run it will bring up the same two strings – Nurohman AR Jun 22 '19 at 01:09
  • See my added comments above. – Idle_Mind Jun 22 '19 at 14:22