3

当我在事件中调用 worker.ReportProgress() 时BackgroundWorker.DoWork,它不起作用,并且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;
}
4

0 回答 0