views:

456

answers:

5

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?

+5  A: 

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

I vote for this answer
PoweRoy
A: 

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.

Wim Haanstra
The OP states BackgroundWorker, and no UI update is shown on the worker thread. My money is on the zeroing (Johan's answer).
Marc Gravell
Yeah, the GUI isn't being updated whilst the work is being done.
Bailz
+4  A: 

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

Bob Moore
Don't you love it when someone accepts your answer without upvoting?
Jon B
A: 

Hi

I have posted an article in CodeProject at http://www.codeproject.com/KB/WPF/WPFWaitProgressBar.aspx - can you please check out and let me know if that helps in your scenario. Thanks

Raja

Raja Lakshman
A: 

Raja, I am having a simular problem with a progressbar in C#.Net. I cannot open your link.

Can you repost the link? thanks!

Cathy