进度条不更新

发布于 2024-07-16 02:09:52 字数 1835 浏览 3 评论 0原文

我有以下代码将数据写入 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

仙气飘飘 2024-07-23 02:09:52

难道(计数/客户)被截断为零(两个整数之间的除法)?

Could it be that (count / customers) is truncated to zero (division between two integers)?

め七分饶幸 2024-07-23 02:09:52

我认为应该是(计数*100)/客户,假设您想要完成百分比。

I think that should be (count*100)/customers, assuming you wanted a percentage complete.

北笙凉宸 2024-07-23 02:09:52

这与线程有关。 因为您正在完成工作的同一线程中更新 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文