我试图将字符串显示到富文本框中,但在第二次运行后总是得到相同或重复的字符串,就像显示第二个字符串的示例一样。
显示第一个字符串的示例:
[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();
如何防止它像示例第二个字符串那样显示重复的字符串?