进度条不更新
我有以下代码将数据写入 XML 文件。
private void WriteResidentData()
{
int count = 1;
status = "Writing XML files";
foreach (Site site in sites)
{
try
{
//Create the XML file
StreamWriter writer = new StreamWriter(path + "\\sites\\" + site.title + ".xml");
writer.WriteLine("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>");
writer.WriteLine("<customer_list>");
foreach (Resident res in site.GetCustomers())
{
bw.ReportProgress((count / customers) * 100);
writer.WriteLine("\t<customer>");
writer.WriteLine("\t\t<customer_reference>" + res.reference + "</customer_reference>");
writer.WriteLine("\t\t<customer_name>" + res.name + "</customer_name>");
writer.WriteLine("\t\t<customer_address>" + res.address + "</customer_address>");
writer.WriteLine("\t\t<payment_method>" + res.method + "</payment_method>");
writer.WriteLine("\t\t<payment_cycle>" + res.cycle + "</payment_cycle>");
writer.WriteLine("\t\t<registered>" + CheckWebStatus(res.reference) + "</registered>");
writer.WriteLine("\t</customer>");
count++;
}
writer.WriteLine("</customer_list>");
writer.Close();
}
catch (Exception ex)
{
lastException = ex;
}
}
}
它使用相同的BackgroundWorker 从数据库获取数据。 我的进度条在从数据库读取时正确显示进度。 然而,在将 XML 写入的进度条归零后,即使该过程正确完成,它也只是停留在 0 处。
谁能建议为什么?
I have the following piece of code to write data to an XML file.
private void WriteResidentData()
{
int count = 1;
status = "Writing XML files";
foreach (Site site in sites)
{
try
{
//Create the XML file
StreamWriter writer = new StreamWriter(path + "\\sites\\" + site.title + ".xml");
writer.WriteLine("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>");
writer.WriteLine("<customer_list>");
foreach (Resident res in site.GetCustomers())
{
bw.ReportProgress((count / customers) * 100);
writer.WriteLine("\t<customer>");
writer.WriteLine("\t\t<customer_reference>" + res.reference + "</customer_reference>");
writer.WriteLine("\t\t<customer_name>" + res.name + "</customer_name>");
writer.WriteLine("\t\t<customer_address>" + res.address + "</customer_address>");
writer.WriteLine("\t\t<payment_method>" + res.method + "</payment_method>");
writer.WriteLine("\t\t<payment_cycle>" + res.cycle + "</payment_cycle>");
writer.WriteLine("\t\t<registered>" + CheckWebStatus(res.reference) + "</registered>");
writer.WriteLine("\t</customer>");
count++;
}
writer.WriteLine("</customer_list>");
writer.Close();
}
catch (Exception ex)
{
lastException = ex;
}
}
}
It's using the same BackgroundWorker that gets the data from the database. My progress bar properly displays the progress whilst it is reading from the database. However, after zeroing the progress bar for the XML writing it simply sits at 0 even though the process is completing correctly.
Can anyone suggest why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
难道(计数/客户)被截断为零(两个整数之间的除法)?
Could it be that (count / customers) is truncated to zero (division between two integers)?
我认为应该是(计数*100)/客户,假设您想要完成百分比。
I think that should be (count*100)/customers, assuming you wanted a percentage complete.
这与线程有关。 因为您正在完成工作的同一线程中更新 GUI。
任务完全完成后进度条是否填满?
嗯,您正在那里使用 bw...所以这可能是您的后台工作进程。
This has to do with threading. Because you are updating your GUI in the same thread as your work is being done.
Does the progressbar fill up when the task is fully complete?
Hmmmzz, you are using a bw in there... so that might be your backgroundworker-process.