title: C# WPF is freezing Progressbar during Datagrid populate
body: <p>I have a slow query. It is taking a time after running. I want to display progressbar during the thread. I tried to use backgroundworker for this.</p>
  
  <p>I am using usercontrol which are my panels children. the picture below is main page. When i click to Page 1 my dockpanel will have page_1 usercontrol.</p>
  
  <p><a href="https://i.stack.imgur.com/ZY9H0.png" rel="nofollow noreferrer">Mainwindow</a>    </p>
  
  <pre><code>    private void button_Click(object sender, RoutedEventArgs e)
      {
          page1 pg = new page1();
          Container_Panel.Children.Add(pg);
      }
  </code></pre>
  
  <p>I am trying to show indetermined progressbar while opening the page1 usercontrol. I use these codes for this.</p>
  
  <pre><code>    private BackgroundWorker bw = new BackgroundWorker();
  
      private void bw_DoWork(object sender, DoWorkEventArgs e)
      {
         Application.Current.Dispatcher.Invoke(new Action(() =&gt;
         {
            populate();
         }));
      }
  
      private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
      {
          prg2.Visibility = Visibility.Hidden;    
      }
  
      private void UserControl_Loaded(object sender, RoutedEventArgs e)
          {
              try
              {
                  bw.WorkerReportsProgress = true;
                  bw.DoWork += bw_DoWork;
                  //bw.ProgressChanged += bw_ProgressChanged;
                  bw.RunWorkerCompleted += bw_RunWorkerCompleted;
                  bw.RunWorkerAsync();
                  prg2.Visibility = Visibility.Visible;
              }
              catch (Exception ex)
              {
                  MessageBox.Show(ex.Message);
              }
          }
  </code></pre>
  
  <p>And the method which has datagrid codes below</p>
  
  <pre><code>private void populate ()
  {
              using (SqlConnection con= new SqlConnection(connectionstring))
              {
                  con.Open();
                  DataTable dt = new DataTable();
                  SqlCommand cmd= new SqlCommand("Select ...", con);
                  cmd.CommandTimeout = 0;
                  SqlDataAdapter _dadapt= new SqlDataAdapter();
                  _dadapt.Fill(dt); 
                  con.Close();
                  dataGrid1.itemsSource = dt.DefaultView;
              }
  }
  </code></pre>
  
  <p>The code is working and i can see prg2 (progressbar) became visible . But it is freezing till the end of the query. After that the other process hide is working and progressbar is hiding. But i want to make progressbar indeterminate during the query.</p>
  
  <p>Or Can i make a progressbar outside of the containerpanel which is working with clicking mainwindows button.</p>
  
  <p>I am trying to solve this issue and i tried too much application but can't solve.</p>