Locked BackgroundWorker_ProgressChanged in a foreach?

  • 31 สิงหาคม 2555 14:32
     
      มีโค้ด

    Hi all,

    I have a backgroundWorker that contains a foreach and essentially goes through a dataSet and loads an image for each row in the set. This appears to be working, but I don't honestly know because when I call backgroundWorker.ReportProgress(100) it doesn't appear to be firing the progress changed until AFTER it goes through the last foreach. I have the ReportProgress call in the foreach by the way, at the end and when setting a breakpoint it does hit that when I would expect it to.

    I guess my question at this point so that I can at least test out all of my other code is how can I fire the progressChanged at the completion of each item in my foreach?

    Below you can see the code that I am using.

    I am instantiating the button (Though this may not be correct)

    ButtonItem button = new ButtonItem();

    BackgroundWorker_DoWork

                foreach (DataRow dr in PicDataSet.Tables["Pictures"].Rows)
                {
                    string AttachmentNameString = dr["AttachmentName"].ToString();
    
                    //ButtonItem button = new ButtonItem();
                    button.Size = new System.Drawing.Size(125, 30);
                    button.Tag = dr["ID"].ToString();
                    button.Text = dr["AttachmentTitle"].ToString();
                    button.ImagePaddingHorizontal = 5;
                    button.ImagePaddingVertical = 5;
    
                    button.ImageFixedSize = new System.Drawing.Size(90, 90);
                    button.ImagePosition = eImagePosition.Top;
    
                    ImagePathString = MappedDriveString + @"\Pictures\Jobs\Thumbnails\" + AttachmentNameString;
    
                    button.Click += this.PictureButton_Click;
    
                    //Populate Image
                    try
                    {
                        using (FileStream fs = new FileStream(ImagePathString, FileMode.Open))
                        {
                            int len = (int)fs.Length;
                            byte[] buf = new byte[len];
                            fs.Read(buf, 0, len);
                            using (MemoryStream ms = new MemoryStream(buf))
                                img = new Bitmap(ms);
    
                            button.Image = img;
    
                            fs.Dispose();
    
                            PicturesBW.ReportProgress(100);
                        }
    
                    }
                    catch
                    {
                        button.Image = ProjectDesk1.Properties.Resources.RedX_128;
                    }
                }

    BackgroundWorker_ProgressChanged (I tried using sender but got a target of an invocation error).

                if (e.ProgressPercentage == 100)
                {
                    //ButtonItem button = sender as ButtonItem;
    
                    AttachmentsItemPanel.BeginUpdate();
    
                    PicturesContainer.SubItems.Add(button);
    
                    AttachmentsItemPanel.EndUpdate();
                }

    BackgroundWorker_Completed

    //Still working here.

    So, I really appreciate the help anyone can give me here. I think my first problem is the foreach issue but if anyone has any suggestions or ideas for any of this I will certainly appreciate any and all input. I do mark answers as well. :)

    Thanks

ตอบทั้งหมด

  • 31 สิงหาคม 2555 22:32
     
     คำตอบ

    Hello:

    You can try this:

     

    private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {

    //In your for loop. Set the max to some high number. I set it to 100.

        for (int i = 0; i <= max; i++)
                {
                    // Report progress to 'UI' thread
                    backgroundWorker.ReportProgress(i); // This fires backgroundWorker_ProgressChanged event handler.
                    // Simulate long task
                    System.Threading.Thread.Sleep(100); // Make the main thread sleep so that when your time taking is running the UI won't hang.
      
          }

            private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
            {

                 // use this e.ProgressPercentage value the way you want. In this it applies this value to the progressbar control.

                progressBar.Value = e.ProgressPercentage;
            }

            private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            {

                     Display any ending/succesful messages here. This case it updates the label. "Status" is a label control.

                status.Text = "Uninstallation Complete!" + "\r\n";
            }

    • ทำเครื่องหมายเป็นคำตอบโดย Lisa ZhuMicrosoft, Moderator 11 กันยายน 2555 2:53
    •  
  • 11 กันยายน 2555 2:53
    ผู้ดูแล
     
     

    Hi C_Newbie01,

    I provisionally marked vasumadhav's reply as answer.

    Please  feel  free to unmark it if you think the information does not help.

    Regards ,


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us