title: Background worker repeats itself
body: <p>Running into another problem with my code.</p>
  
  <p>Scenario:</p>
  
  <p>I use a background worker to populate a listView with data from an XML file. Everything works fine, however when I populate the listView using the background worker it repeats itself over and over, usually 3 - 5 times. Here is my full code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Globalization;
  using System.Linq;
  using System.Text;
  using System.Threading;
  using System.Threading.Tasks;
  using System.Windows.Forms;
  using System.Xml.Linq;
  
  namespace THC_Poker_Notifier
  {
      public partial class Main : Form
      {
          public Main()
          {
              InitializeComponent();
          }
  
          int FPPFee = 0;
          string StartDate;
  
          // Control Invike functions for multi-threading
  
          delegate void UniversalVoidDelegate();
  
          public static void ControlInvike(Control control, Action function)
          {
              if (control.IsDisposed || control.Disposing)
                  return;
  
              if (control.InvokeRequired)
              {
                  control.Invoke(new UniversalVoidDelegate(() =&gt; ControlInvike(control, function)));
                  return;
              }
              function();
          }
  
          private void exitToolStripMenuItem_Click(object sender, EventArgs e)
          {
              Application.Exit();
          }
  
          private void supportThisProjectToolStripMenuItem_Click(object sender, EventArgs e)
          {
              SupportProject donate = new SupportProject();
              donate.ShowDialog();
          }
  
          private void Main_Load(object sender, EventArgs e)
          {
  
              backgroundWorker1.WorkerSupportsCancellation = true;
              backgroundWorker1.WorkerReportsProgress = true;
  
              backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
              backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
              backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
  
              if (backgroundWorker1.IsBusy != true)
              {
                  backgroundWorker1.RunWorkerAsync();
              }
  
          }
  
          private void LoadAllTournaments()
          {
  
              ControlInvike(listView1, () =&gt; listView1.Items.Clear());
  
              DataSet ds = new DataSet();
              ds.ReadXml("https://www.pokerstars.com/datafeed/tournaments/all.xml");
  
              ListViewItem item;
  
              foreach (DataRow dr in ds.Tables["tournament"].Rows)
              {
  
                  DateTimeOffset startDate = DateTimeOffset.ParseExact((string)dr["start_date"], "yyyy-MM-dd'T'HH:mm:sszzz", CultureInfo.InvariantCulture);
  
                  TimeZone localZone = TimeZone.CurrentTimeZone;
  
                  TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById(localZone.StandardName.ToString());
                  DateTimeOffset converted = TimeZoneInfo.ConvertTime(startDate, tz);
  
                  if (dr["play_money"].ToString() != "true")
                  {
  
                      FPPFee = Convert.ToInt32(dr["fpp_fee"]);
  
                      if (FPPFee == 0)
                      {
  
                          if (dr["buy_in_fee"].ToString() != "$0 + $0")
                          {
  
                              item = new ListViewItem(new string[] { dr["name"].ToString(), converted.ToString("d"), converted.ToString("t"), dr["buy_in_fee"].ToString(), dr["prize"].ToString(), dr["players"].ToString(), dr["status"].ToString(), dr["id"].ToString() });
                              ControlInvike(listView1, () =&gt; listView1.Items.Add(item));
                              Thread.Sleep(10);
                          }
  
                      }
  
                  }
  
              }
  
          }
  
          private void LoadRegularTournaments()
          {
  
              ControlInvike(listView2, () =&gt; listView2.Items.Clear());
  
              DataSet ds = new DataSet();
              ds.ReadXml("http://46.101.5.145/Feeds/regular.xml");
  
              ListViewItem item2;
  
              foreach (DataRow dr in ds.Tables["tournament"].Rows)
              {
  
                  StartDate = dr["start_date"].ToString();
  
                      FPPFee = Convert.ToInt32(dr["fpp_fee"]);
  
                      if (FPPFee == 0)
                      {
  
                          if (dr["buy_in_fee"].ToString() != "$0 + $0")
                          {
  
                              item2 = new ListViewItem(new string[] { dr["name"].ToString(), StartDate.Substring(0, 10), StartDate.Substring(12, 7), dr["buy_in_fee"].ToString(), dr["prize"].ToString(), dr["players"].ToString(), dr["status"].ToString(), dr["id"].ToString() });
                              ControlInvike(listView2, () =&gt; listView2.Items.Add(item2));
  
  
                          }
  
                      }
  
              }
  
          }
  
          private void LoadSatelliteTournaments()
          {
  
              ControlInvike(listView3, () =&gt; listView3.Items.Clear());
  
              DataSet ds = new DataSet();
              ds.ReadXml("http://46.101.5.145/Feeds/satellite.xml");
  
              ListViewItem item3;
  
              foreach (DataRow dr in ds.Tables["tournament"].Rows)
              {
  
                  StartDate = dr["start_date"].ToString();
  
                      FPPFee = Convert.ToInt32(dr["fpp_fee"]);
  
                      if (FPPFee == 0)
                      {
  
                          if (dr["buy_in_fee"].ToString() != "$0 + $0")
                          {
  
                              item3 = new ListViewItem(new string[] { dr["name"].ToString(), StartDate.Substring(0, 10), StartDate.Substring(12, 7), dr["buy_in_fee"].ToString(), dr["prize"].ToString(), dr["players"].ToString(), dr["status"].ToString(), dr["id"].ToString() });
                              ControlInvike(listView3, () =&gt; listView3.Items.Add(item3));
  
  
                          }
  
                      }
  
              }
  
          }
  
          private void LoadSpecialTournaments()
          {
  
              ControlInvike(listView4, () =&gt; listView4.Items.Clear());
  
              DataSet ds = new DataSet();
              ds.ReadXml("http://46.101.5.145/Feeds/special.xml");
  
              ListViewItem item4;
  
              foreach (DataRow dr in ds.Tables["tournament"].Rows)
              {
  
                  StartDate = dr["start_date"].ToString();
  
                  FPPFee = Convert.ToInt32(dr["fpp_fee"]);
  
                  if (FPPFee == 0)
                  {
  
                      if (dr["buy_in_fee"].ToString() != "$0 + $0")
                      {
  
                          item4 = new ListViewItem(new string[] { dr["name"].ToString(), StartDate.Substring(0, 10), StartDate.Substring(12, 7), dr["buy_in_fee"].ToString(), dr["prize"].ToString(), dr["players"].ToString(), dr["status"].ToString(), dr["id"].ToString() });
                          ControlInvike(listView4, () =&gt; listView4.Items.Add(item4));
  
  
                      }
  
                  }
  
              }
  
          }
  
          private void LoadFreerollTournaments()
          {
  
              ControlInvike(listView5, () =&gt; listView5.Items.Clear());
  
              DataSet ds = new DataSet();
              ds.ReadXml("http://46.101.5.145/Feeds/freeroll.xml");
  
              ListViewItem item5;
  
              foreach (DataRow dr in ds.Tables["tournament"].Rows)
              {
  
                  if (dr["play_money"].ToString() != "true")
                  {
  
                      StartDate = dr["start_date"].ToString();
  
                      FPPFee = Convert.ToInt32(dr["fpp_fee"]);
  
                      if (FPPFee == 0)
                      {
  
                          if (dr["buy_in_fee"].ToString() != "$0 + $0")
                          {
  
                              item5 = new ListViewItem(new string[] { dr["name"].ToString(), StartDate.Substring(0, 10), StartDate.Substring(12, 7), dr["buy_in_fee"].ToString(), dr["prize"].ToString(), dr["players"].ToString(), dr["status"].ToString(), dr["id"].ToString() });
                              ControlInvike(listView5, () =&gt; listView5.Items.Add(item5));
  
  
                          }
  
                      }
  
                  }
  
              }
  
          }
  
          private void LoadFPPTournaments()
          {
  
              ControlInvike(listView6, () =&gt; listView6.Items.Clear());
  
              DataSet ds = new DataSet();
              ds.ReadXml("http://46.101.5.145/Feeds/all.xml");
  
              ListViewItem item6;
  
              foreach (DataRow dr in ds.Tables["tournament"].Rows)
              {
  
                  if (dr["play_money"].ToString() != "true")
                  {
  
                      StartDate = dr["start_date"].ToString();
  
                      FPPFee = Convert.ToInt32(dr["fpp_fee"]);
  
                      if (FPPFee != 0)
                      {
  
                          item6 = new ListViewItem(new string[] { dr["name"].ToString(), StartDate.Substring(0, 10), StartDate.Substring(12, 7), dr["buy_in_fee"].ToString(), dr["prize"].ToString(), dr["players"].ToString(), dr["status"].ToString(), dr["id"].ToString() });
                          ControlInvike(listView6, () =&gt; listView6.Items.Add(item6));
  
  
                      }
  
                  }
  
              }
  
          }
  
          private void LoadPlayMoneyTournaments()
          {
  
              ControlInvike(listView7, () =&gt; listView7.Items.Clear());
  
              DataSet ds = new DataSet();
              ds.ReadXml("http://46.101.5.145/Feeds/freeroll.xml");
  
              ListViewItem item7;
  
              foreach (DataRow dr in ds.Tables["tournament"].Rows)
              {
  
                  if (dr["play_money"].ToString() == "true")
                  {
  
                      StartDate = dr["start_date"].ToString();
  
                      FPPFee = Convert.ToInt32(dr["fpp_fee"]);
  
                      if (FPPFee == 0)
                      {
  
                          item7 = new ListViewItem(new string[] { dr["name"].ToString(), StartDate.Substring(0, 10), StartDate.Substring(12, 7), dr["buy_in_fee"].ToString(), dr["prize"].ToString(), dr["players"].ToString(), dr["status"].ToString(), dr["id"].ToString() });
                          ControlInvike(listView7, () =&gt; listView7.Items.Add(item7));
  
  
                      }
  
                  }
  
              }
  
          }
  
          private void timerUpdateGames_Tick(object sender, EventArgs e)
          {
  
              backgroundWorker1.WorkerSupportsCancellation = true;
              backgroundWorker1.WorkerReportsProgress = true;
  
              backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
              backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
              backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
  
              if (backgroundWorker1.IsBusy != true)
              {
                  backgroundWorker1.RunWorkerAsync();
              }
  
          }
  
          private void pictureBox3_MouseEnter(object sender, EventArgs e)
          {
              this.Cursor = Cursors.Hand;
          }
  
          private void pictureBox3_MouseLeave(object sender, EventArgs e)
          {
              this.Cursor = Cursors.Arrow;
          }
  
          private void pictureBox3_Click(object sender, EventArgs e)
          {
  
              backgroundWorker1.WorkerSupportsCancellation = true;
              backgroundWorker1.WorkerReportsProgress = true;
  
              backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
              backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
              backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
  
              if (backgroundWorker1.IsBusy != true)
              {
                  backgroundWorker1.RunWorkerAsync();
              }
  
          }
  
          private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
          {
  
              BackgroundWorker worker = sender as BackgroundWorker;
  
              LoadAllTournaments();
              LoadRegularTournaments();
              LoadSatelliteTournaments();
              LoadSpecialTournaments();
              LoadFreerollTournaments();
              LoadFPPTournaments();
              LoadPlayMoneyTournaments();
  
          }
  
          private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
          {
          }
  
          private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
          {
              if ((e.Cancelled == true))
              {
              }
  
              else if (!(e.Error == null))
              {
                  MessageBox.Show("Error: " + e.Error.Message);
              }
  
              else
              {
              }
          }
  
      }
  }
  </code></pre>
  
  <p>Does anyone have any idea why the listView would be populated more than once? I wouldn't mind, however it removes the items while it's updating so I cannot access the list for several minutes.</p>