我从文件中读取数据并将它们写入其他文件。我想用标签显示用户读取行数。它工作。用户可以停止阅读。我使用 backgroundworke,但应用程序不响应停止按钮 - 被冻结。我尝试了 Aplication.DoEvents(),但我必须只需要按 2x 来响应。
开始阅读
private void button1_Click(object sender, EventArgs e)
{
string ext = Path.GetExtension(openFileDialog1.FileName);
if (ext == ".arff")
{
getColumn();
backgroundWorker1.RunWorkerAsync();
}
}
读写
private void readDataArffBig()
{
int lines = 0;
StreamWriter sw = new StreamWriter(tempFile, true);
using (StreamReader sr = new StreamReader(openFileDialog1.FileName))
{
while ((line = sr.ReadLine()) != null)
{
lines++;
backgroundWorker1.ReportProgress(lines);
if (status == false)
break;
sw.WriteLine(line)}
}
}
停止阅读将状态设置为 false..berofe 为 true
private void stop_Click(object sender, EventArgs e)
{
status = false;
}
BW使用
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
readDataArffBig();
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
label8.Text = e.ProgressPercentage.ToString();
label8.Refresh();
}