locked
Why when reporting progress in dowork of backgroundworker the result is number of files - 1 ? RRS feed

  • Question

  • private void Worker_DoWork(object sender, DoWorkEventArgs e)
            {
                for (int i = 0; i < directories.Count; i++)
                {
                    BitmapImage imagetosave = ResizeImage(directories[i]);
                    Save(imagetosave, saveDirectory);
    
                    int percents = ((i + 1) * 100) / directories.Count;
                    worker.ReportProgress(percents, i);              
                }
            }
    
            private void Worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
            {
                progressBar1.Value = e.ProgressPercentage;
                label.Content = e.ProgressPercentage.ToString() + "%";
                label2.Content = e.UserState;
                listView.Items.RemoveAt((int)e.UserState);
                listView.Items.Insert((int)e.UserState, "Resized and saved");
            }

    For example if directories contains 2 items in the end of the process label2 will show the number 1 and not 2.

    Or if directories that is a List<string> contains 46 files then in the end label2 will show 45 and not 46.

    What i tried so far is in the DoWork to change in the loop to i <= directories.Count

    Then i tried to remove the (i + 1) in the int percents.

    I tried to add + 1 to the ReportProgress line (percents, i + 1)

    But so far nothing helped. 

    Monday, September 18, 2017 2:17 AM

All replies

  • Hi,

    Sorry that I can not test your code because there are lots of unknown variables in it, so could you please provide more details and the complete code for your question?

    Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, September 18, 2017 8:41 AM