title: Using the Progress Bar in C# for loading images from folder?
body: <p>One part of my application works loading of Images from the predefined folder. At this time when loading the images it takes more time. Now I need a progress Bar which can let me to tell the progress of loading. But I don't know how to use it specially with loading images.</p>
  
  <p>The function works by importing images into the controls called: ImageList and Listview. The imageList will populate the listview. I think these controls are the main routine work for the delay to display the image:</p>
  
  <pre><code>this.lstView_un.View = View.LargeIcon;
                  lstView_un.LargeImageList = this.imageList1;
                  lstView_un.Items.Clear();
                  for (int j = 0; j &lt; this.imageList1.Images.Count; j++)
                  {
                      ListViewItem item = new ListViewItem();
                      item.ImageIndex = j;
                      item.Text = imageList1.Images.Keys[j].ToString();
                      this.lstView_un.Items.Add(item);
                  }
  </code></pre>
  
  <blockquote>
    <p>See Bellow Codes for detail</p>
  </blockquote>
  
  <p>Any one can help me?
  Thanks in Advance!</p>
  
  <p>I Have the following code to Load Images from folder.</p>
  
  <pre><code>private void LoadImages()
  {
   try
              {
                  lstView_un.Items.Clear();
                  lstView_un.LargeImageList.Images.Clear();
                  DateTime d1 = DateTime.Now;
                  DateTime d2 = dtp_unlist.Value;
  
                  TimeSpan t = d1 - d2;
                  double NrOfDays = t.TotalDays;
                  if (d2 &gt; d1)
                  {
  
                      MessageBox.Show("Please select before the current date!");
                      return;
  
                  }
  
                  DateTime dt = DateTime.Now.AddDays(-NrOfDays);
                  lstView_un.Items.Clear();
                  string imagesPath = (Application.StartupPath + "/UnknownFaces/");
                  string[] extensions = new[] { ".jpg", ".jpeg", ".png" };
                  var allfiles = Directory.GetFiles(imagesPath);
                  List&lt;FileInfo&gt; files = new List&lt;FileInfo&gt;();
                  foreach (string f in allfiles) files.Add(new FileInfo(f));
                  var filesSorted = files.Where(f =&gt; extensions.Contains(f.Extension.ToLower()))
                                         .Where(f =&gt; f.CreationTime &lt; dt)
                                         .OrderByDescending(f =&gt; f.CreationTime);
                  this.imageList1.ImageSize = new Size(256, 256);
                  this.imageList1.ColorDepth = ColorDepth.Depth32Bit;
                  foreach (FileInfo fileInfo in filesSorted)
                  {
                      try
                      {
                          this.imageList1.Images.Add(fileInfo.Name,
                                                   Image.FromFile(fileInfo.FullName));
                      }
                      catch
                      {
                          Console.WriteLine(fileInfo.FullName + "  is is not a valid image.");
                      }
                  }
                  this.lstView_un.View = View.LargeIcon;
                  lstView_un.LargeImageList = this.imageList1;
                  lstView_un.Items.Clear();
                  for (int j = 0; j &lt; this.imageList1.Images.Count; j++)
                  {
                      ListViewItem item = new ListViewItem();
                      item.ImageIndex = j;
                      item.Text = imageList1.Images.Keys[j].ToString();
                      this.lstView_un.Items.Add(item);
                  }
  
              }
              catch (Exception ex)
              {
                  MessageBox.Show("Something Wrong happen! "+ex.Message);
              }
          }
  }
  </code></pre>
  
  <p>I have the following Progress bar on the current Image loading form:</p>
  
  <pre><code>private void progressBar1_Click(object sender, EventArgs e)
          {
  
          }
  </code></pre>
  
  <p>And I tried as Well: (It  is not work properly)</p>
  
  <pre><code>private void timer1_Tick(object sender, EventArgs e)
          {
              if (this.progressBar1.Value &lt; 50) // Should be less than progress bar max value
              {
                  this.progressBar1.Value++;
                  if (this.progressBar1.Value == 100) //The maximum value of the progress bar
                  {
                      LoadImages();// Image loads when reached the max value
                  }
              }
              else
              {
                  this.timer1.Enabled = false;
              } 
          }
  </code></pre>
  
  <p>I think Background worker is good, but I don't know how to use it.
  Please any one can help me, if you know it.</p>