reportWorker中的reportProgress在操作完成之前无法工作

时间:2012-04-10 03:37:26

标签: c# backgroundworker

当我在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;
}

0 个答案:

没有答案