title: Closing a wpf window created on a different thread from the background worker WorkRunCompleted
body: <p>Hi there is a follow up to another question <a href="https://stackoverflow.com/questions/19334583/progress-bar-update-from-background-worker-stalling">here</a></p>
  
  <p>Here is the summary</p>
  
  <p>In a wpf application in c# I had a long process of updating a remote database. to do this I created a background worker. However I wanted to have a window open and a progressbar run during the database update routine. All my attempts to achieve this on my main window with a progressbar set to Indeterminate failed as the "swishing" effect on my progressbar did not start to run on my main window till after my background worker thread had finished</p>
  
  <p>Any with help and this article <a href="http://www.codeproject.com/Articles/449594/Progress-Bars-Threads-Windows-Forms-and-You" rel="nofollow noreferrer">here</a> I managed to open a new window in a different thread and run the background worker and have the progressbar "swish" properly and the background work run to completion. HOWEVER, my new question is</p>
  
  <p><strong>How do I close my progressbar window (called progressDialog) once my background work is finished?</strong></p>
  
  <p>Please bear in mind that I am quite new to this and code examples would be greatly apprieciated and I THINK, but I am not certain I am going to want to close the progressbar window from the background worker RunWorkerCompleted area of code</p>
  
  <p>here is my code</p>
  
  <p>I set up the background worker as</p>
  
  <pre><code>  public partial class MainWindow : Window
  {
      //Declare background workers
      BackgroundWorker bwLoadCSV = new BackgroundWorker();
  
  
      //Declare class variables
      // some stuff
  
      public MainWindow()
      {
          InitializeComponent();
          //assign events to backgroundworkers
          bwLoadCSV.WorkerReportsProgress = true;
          bwLoadCSV.WorkerSupportsCancellation = true;
          bwLoadCSV.DoWork += new DoWorkEventHandler(bwLoadCSV_DoWork);
          bwLoadCSV.ProgressChanged += new ProgressChangedEventHandler(bwLoadCSV_ProgressChanged);
          bwLoadCSV.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bwLoadCSV_RunWorkerCompleted);
        }
    }
  </code></pre>
  
  <p>I run the event on a button click event as;</p>
  
  <pre><code>private void CSV_Load_Click(object sender, RoutedEventArgs e)
      ///Function to read csv into datagrid
      ///
      {
          //Turn Cursor to wait
          System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
          Thread backgroundThread = new Thread(
      new ThreadStart(() =&gt;
      {
          ProgressDialog progressDialog = new ProgressDialog();
          progressDialog.tbEvent.Text = "Loading CSV Data....";
          progressDialog.progressBar1.IsIndeterminate = true;
          progressDialog.ShowDialog();
      }
  
  ));
          backgroundThread.SetApartmentState(ApartmentState.STA);
          backgroundThread.Start();
  
          //Test connection to sql server
          if (CHHoursDataProvider.IsDatabaseOnline() == false)
          {
              System.Windows.Forms.MessageBox.Show("Can not establish contact with sql server" + "\n" + "Contact IT", "Connection Error");
              //Set UI picture
              return;
          }
          //Set a control to update the user here
          tbLoadDgStat.Visibility = Visibility.Visible;
  
          //tbLoadDgStat.Text = "Getting data templete from Database...";
          string FilePath = txFilePath.Text;
          if (bwLoadCSV.IsBusy != true)
          {
              //load the context object with parameters for Background worker
              bwCSVLoadContext Context = new bwCSVLoadContext();
              Context.Site = cBChSite.Text;
              Context.FilePath = txFilePath.Text;
              Context.FileName = fileTest;
              Context.Wageyear = cbWageYear.Text;
              Context.Startdate = ((DateTime)dpStartDate.SelectedDate);
              Context.Enddate = ((DateTime)dpEndDate.SelectedDate);
  
              bwLoadCSV.RunWorkerAsync(Context);                
           }
      }
  </code></pre>
  
  <p>My progressbar form progressDialog xaml and class are this;</p>
  
  <pre><code>&lt;Window x:Class="Test_Read_CSV.ProgressDialog"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="Progress Dialog" Height="115" Width="306" Name="ProgressPopup"&gt;
  &lt;Grid&gt;
      &lt;ProgressBar Height="31" HorizontalAlignment="Left" Margin="12,33,0,0" Name="progressBar1" VerticalAlignment="Top" Width="250" x:FieldModifier="public" /&gt;
      &lt;TextBox Height="23" HorizontalAlignment="Left" Margin="7,4,0,0" Name="tbEvent" VerticalAlignment="Top" Width="254" IsReadOnly="True" IsEnabled="False" x:FieldModifier="public" /&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <pre><code>class is
   public partial class ProgressDialog : Window
  {
      public ProgressDialog()
      {
          WindowStartupLocation = WindowStartupLocation.CenterScreen;
          InitializeComponent();
          progressBar1.IsIndeterminate = true;
  
      }
  </code></pre>
  
  <p>My background worker completed code is</p>
  
  <pre><code>  private void bwLoadCSV_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
      {
          bwCSVLoadContext Context = e.Result as bwCSVLoadContext;
          Thread.Sleep(5000);
          if ((e.Cancelled == true))
          {
  
              this.tbLoadDgStat.Text = "Canceled!";
              System.Threading.Thread.Sleep(1000);
  
              System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
              tbLoadDgStat.Visibility = Visibility.Hidden;
          }
  
          else if (!(e.Error == null))
          {
              //this.tbProgress.Text = ("Error: " + e.Error.Message);
  
          }
  
          else
          {
              if (Context.LoadResult == true)
              {
                  this.dgCSVData.DataContext = oTable.DefaultView;
                  btUpload.IsEnabled = true;
              }
  
              **//close the progressbar window some how here!!!**
  
  
              //On the main window
              this.tbLoadDgStat.Text = "Complete";
              progressBar1.Value = 100;
  
  
              System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
              tbLoadDgStat.Visibility = Visibility.Hidden;
          }
  
  
  
          System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
  
      }
  </code></pre>