当我在BackgroundWorker.DoWork
事件中调用worker.ReportProgress()时,它将无效,ProgressChanged
事件仅在for
循环结束时引发。
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
string writePath = Path.Combine(bookPath, "bytes");
DataTable dt = new DataTable();
dt = Form1.book.Select("select * from book");
Directory.CreateDirectory(writePath);
foreach (DataRow dr in dt.Rows)
{
int pagenumber = Convert.ToInt32(dr["pageNumber"]);
string text = dr["text"].toString();
byte[] codes = tocode.convertTobyte(text.ToCharArray());
File.WriteAllBytes(Path.Combine(writePath, pagenumber.ToString()),codes);
backgroundWorker1.ReportProgress((pagenumber * 100) / dt.Rows.Count);
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBarMain.Value = e.ProgressPercentage;
}