title: Using background Worker to insert Text file into a listBox WPF
body: <p>When I add this line of code inside loop </p>
  
  <pre><code>myCollection.Add(new FetchData(line)); 
  </code></pre>
  
  <p>it throws </p>
  
  <blockquote>
    <p>This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread</p>
  </blockquote>
  
  <p>I have also tried using this, but didn't work:</p>
  
  <pre><code>mqttUpload.ItemsSource = MyList;
  </code></pre>
  
  <p>The Progress Bar updates and I am also able to print lines, the only problem is that I cannot add the data to the ListBox. </p>
  
  <p>This is how  my data looks like inside the txt file, this file contains 3500 lines.
  <a href="https://i.stack.imgur.com/abVD6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/abVD6.png" alt="enter image description here"></a>    </p>
  
  <p>Here is what i have tried. </p>
  
  <pre><code>public partial class UserControl2 : UserControl
  {
  
      public static List&lt;string&gt; Mylist = new List&lt;string&gt;();
      ObservableCollection&lt;FetchData&gt; myCollection = new  ObservableCollection&lt;FetchData&gt;();
  
      public UserControl2()
      {
          InitializeComponent();
          mqttUpload.ItemsSource = myCollection;
  
          backgroundWorker1.DoWork += backgroundWorker1_DoWork;
          backgroundWorker1.ProgressChanged +=backgroundWorker1_ProgressChanged;
          backgroundWorker1.WorkerReportsProgress = true;
          backgroundWorker1.RunWorkerCompleted += jobfinish;
  
      }
  
      public static UserControl2 uploadData;
  
      BackgroundWorker backgroundWorker1 = new BackgroundWorker();
      public static string FilePath = "";
  
      private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
      {
          var files = File.ReadAllLines(FilePath);
          for (int i = 0; i &lt; files.Length; i++)
          {
              var line = files[i];
              Mylist.Add(line);
              int percentage = (int)((i / (double)files.Length) * 100.0);
              backgroundWorker1.ReportProgress(percentage);
              Console.WriteLine(line);
          }
      }
  
      public void jobfinish(object sender, RunWorkerCompletedEventArgs e)
      {
          Console.WriteLine(  "finish");
  
      }
  
      private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)// progress change
      {
          progressBar1.Value = e.ProgressPercentage;
      }
  }
  </code></pre>
  
  <p>Fetch class</p>
  
  <pre><code>public class FetchData
  {
      public string fetchData { get; set; }
  
      public FetchData(string fetchData)
      {
          this.fetchData = fetchData;
      }
  }
  </code></pre>
  
  <p>Here is XAML code</p>
  
  <pre><code>&lt;ListBox Margin="10,64,0,-434" Grid.Column="2" Name="mqttUpload" 
           Padding="20,0,0,0" Background="#18191A"  Foreground="#F5980C" Grid.Row="2" /&gt;
  </code></pre>
  
  <p>My goal is to read data line by line while showing progress bar and finally show data inside the ListBox.</p>
  
  <p>This is how data suppose to be shown in the Listbox. in this image i am reading data from database. i want to achieve same but reading from txt file.
  <a href="https://i.stack.imgur.com/eTSDt.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/eTSDt.png" alt="enter image description here"></a></p>