-2

我试图将字符串显示到富文本框中,但在第二次运行后总是得到相同或重复的字符串,就像显示第二个字符串的示例一样。

显示第一个字符串的示例:

[21:55:07] - [#] 开始扫描,请稍候。. .

[21:55:09] - [√] 完成。

显示第二个字符串的示例:

[21:55:07] - [#] 开始扫描,请稍候。. .

[21:55:09] - [√] 完成。

[21:55:07] - [#] 开始扫描,请稍候。. .

[21:55:09] - [√] 完成。

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

如何防止它像示例第二个字符串那样显示重复的字符串?

4

1 回答 1

1

我〜想〜你只是想让 RichTextBox 在再次开始扫描时重新开始?

如果是这样,请在开头使用Clear()startScan()方法:

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

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

}

- - - - - 编辑 - - - - -

每次执行扫描时,您都在连接后台工作程序的事件。这应该只做一次。一个好地方是表单的 Load() 事件。

这些是需要移动的行,并且只调用一次:

worker.DoWork += Scan;
worker.ProgressChanged += this.ProgressChanged;
worker.RunWorkerCompleted += this.RunWorkerCompleted;
于 2019-06-20T15:31:56.057 回答