private void btnProcessMultipleCourts_Click(object sender, RoutedEventArgs e)
        {
            // If the background thread is running then clicking this
            // button causes a cancel, otherwise clicking this button
            // launches the background thread.
            _multipleTransactions = new List <PacerImportTransaction>();

            if (_asyncWorker.IsBusy)
            {
                btnProcessMultipleCourts.IsEnabled = false;
                lblProcessingStatus.Content        = "Canceling...";

                // Notify the worker thread that a cancel has been requested.
                // The cancel will not actually happen until the thread in the
                // DoWork checks the bwAsync.CancellationPending flag, for this
                // reason we set the label to "Canceling...", because we haven't
                // actually cancelled yet.

                _asyncWorker.CancelAsync();
            }
            else
            {
                if (grdCourts.SelectedItems == null || grdCourts.SelectedItems.Count == 0)
                {
                    MessageBox.Show("Please Select a Court!");
                    return;
                }
                else
                {
                    btnProcessMultipleCourts.Content = "Cancel";
                    lblProcessingStatus.Content      = "Processing...";

                    MutipleProcessSpec _spec = new MutipleProcessSpec();
                    _spec.Courts = grdCourts.SelectedItems.ToList();
                    radProgressBarDownload.Maximum = grdCourts.SelectedItems.Count;

                    if (rdoUseSpecifiedDate.IsChecked == true)
                    {
                        _spec.StartDate = rdpStartDate.SelectedDate;
                        _spec.EndDate   = rdpEndDate.SelectedDate;
                    }

                    _spec.FiledOnly        = (bool)rdoFiled.IsChecked;
                    _spec.GeocodeAddresses = (bool)chkGeocodeAddresses.IsChecked;

                    // Kickoff the worker thread to begin it's DoWork function.
                    _asyncWorker.RunWorkerAsync(_spec);
                }
            }
        }
        private void bwAsync_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                // The Sender is the BackgroundWorker object we need it to
                // report progress and check for cancellation.
                BackgroundWorker   bwAsync = sender as BackgroundWorker;
                MutipleProcessSpec _spec   = (MutipleProcessSpec)e.Argument;

                int    i      = 0;
                string _state = "Checking ECF Versions...";
                bwAsync.ReportProgress(0, _state);

                _state = CourtService.CheckECFVersions();
                bwAsync.ReportProgress(0, _state);
                Thread.Sleep(1000);

                foreach (object _court in _spec.Courts)
                {
                    i++;

                    //refresh court from ID to get version that may have been updated
                    CourtService.Refresh((Court)_court);

                    if ((_spec.FiledOnly == true && (((Court)_court).LastPacerLoadFileDate == DateTime.Parse(DateTime.Now.AddDays(-1).ToShortDateString()))) ||
                        (_spec.FiledOnly == false && (((Court)_court).LastPacerLoadDischargeDate == DateTime.Parse(DateTime.Now.AddDays(-1).ToShortDateString()))))
                    {
                        //do nothing
                    }
                    else
                    {
                        // Periodically report progress to the main thread so that it can
                        // update the UI.
                        _state = "Downloading cases for  " + ((Court)_court).CourtName + "...";
                        bwAsync.ReportProgress(Convert.ToInt32(i * (100.0 / _courts.Count)), _state);
                        PacerImportTransaction _transaction = new PacerImportTransaction();

                        _transaction.CourtID         = ((Court)_court).ID;
                        _transaction.CourtName       = ((Court)_court).CourtName;
                        _transaction.DischargedCases = !_spec.FiledOnly;

                        if (_spec.StartDate != null)
                        {
                            _transaction.StartDate = (DateTime)_spec.StartDate;
                        }
                        //filed only cases use the LastPacerLoadFileDate
                        else if (_spec.FiledOnly == true)
                        {
                            if (((DateTime)((Court)_court).LastPacerLoadFileDate) > DateTime.MinValue)
                            {
                                _transaction.StartDate = ((DateTime)((Court)_court).LastPacerLoadFileDate).AddDays(1);
                            }
                            else
                            {
                                _transaction.StartDate = DateTime.Now.AddMonths(-1);
                            }
                        }
                        //discharged cases use the LastPacerLoadFileDate
                        else if (_spec.FiledOnly == false)
                        {
                            if (((DateTime)((Court)_court).LastPacerLoadDischargeDate) > DateTime.MinValue)
                            {
                                _transaction.StartDate = ((DateTime)((Court)_court).LastPacerLoadDischargeDate).AddDays(1);
                            }
                            else
                            {
                                _transaction.StartDate = DateTime.Now.AddMonths(-1);
                            }
                        }

                        if (_spec.EndDate != null)
                        {
                            _transaction.EndDate = (DateTime)_spec.EndDate;
                        }
                        else
                        {
                            _transaction.EndDate = DateTime.Parse(DateTime.Now.AddDays(-1).ToShortDateString());
                        }

                        _transaction.PacerFileFormatID = ((Court)_court).PacerFileFormatID;

                        //check if the transaction overlaps prior periods to avoid extra chanrges, if so throw an error
                        _transaction.CheckForPriorOverlappingDownloads();

                        _transaction.DownloadNewCases(_spec.GeocodeAddresses);

                        _multipleTransactions.Add(_transaction);

                        // Periodically check if a Cancellation request is pending.  If the user
                        // clicks cancel the line _asyncWorker.CancelAsync();
                        if (bwAsync.CancellationPending)
                        {
                            // Pause for bit to demonstrate that there is time between
                            // "Cancelling..." and "Canceled".
                            // Thread.Sleep(1200);

                            // Set the e.Cancel flag so that the WorkerCompleted event
                            // knows that the process was canceled.
                            e.Cancel = true;
                            return;
                        }
                    }
                }
                bwAsync.ReportProgress(100);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }