后台工作程序RunWorker已完成事件甚至在执行工作事件之前执行

时间:2015-02-11 19:02:01

标签: c# wpf backgroundworker

我有WPF应用程序,在Ui我有一个标题复选框,当它被选中时,检查所有正文复选框。当选中任何复选框时,我通过调用完美的后台工作程序来完成我需要的工作。如果我单独选中任何复选框而不检查标题,那么后台工作程序工作正常。

问题是当我检查标题时,检查了所有正文复选框,在这种情况下,即使在执行'do work'之前,正在执行后台工作程序'Run Worker completed'事件,这会破坏我的代码。请帮忙。这是我的代码。 RunJob来自前端按钮的命令。

public void RunJob(object obj)
        {
            obforSave = obj;
            workerforRun = new BackgroundWorker();
            workerforRun.WorkerReportsProgress = true;
            workerforRun.DoWork += workerforRun_DoWork;
            workerforRun.ProgressChanged += workerforRun_ProgressChanged;
            workerforRun.RunWorkerCompleted += workerforRun_RunWorkerCompleted;
            if (!workerforRun.IsBusy)
            {
                workerforRun.RunWorkerAsync();
            }
        }

        //Special case header is checked Runwroker completed is executing before do work.
        void workerforRun_DoWork(object sender, DoWorkEventArgs e)
        {
            IsValidationsComplete = false;
            int count = 0;
            //var obj = e.Argument;
            if (IsRunJobAlreadyExecuted == false)
            {
                ResultExtract = JobEntities.Any(s => s.ExtractIsSelected != null && (bool)s.ExtractIsSelected);
                var resultTransform = JobEntities.Any(x => x.TransformIsSelected != null && (bool)x.TransformIsSelected);
                var resultLoad = JobEntities.Any(b => b.LoadIsSelected != null && (bool)b.LoadIsSelected);
                //Check if any of the entities are either Extracted, Transformed or Loaded.
                if (ResultExtract || resultTransform || resultLoad)
                {
                    SaveJobConfigurationChanges(obforSave);
                    var jobEngine = new JobEngine();
                    var jobId = JobEntities[0].JobId;
                    jobEngine.ProcessJob(jobId);
                    MessageBox.Show("Job Execution Complete", "Run Job");
                    AllResults = GetJobConfigurationResults();
                    foreach (var item in AllResults)
                    {
                        if (item.ExtractIsSelected == true && item.ExtractStatus == "Completed Successfully")
                        {
                            count += Convert.ToInt32(item.ExtractRowsSelected);
                        }
                        if (item.TransformIsSelected == true && item.TransformStatus == "Completed Successfully")
                        {
                            count += Convert.ToInt32(item.TransformRowsSelected);
                        }
                        if (item.LoadIsSelected == true && item.LoadStatus == "Completed Successfully")
                        {
                            count += Convert.ToInt32(item.LoadRowsSelected);
                        }
                    }

                    workerforRun.ReportProgress(count);
                    //MessageBox.Show(count.ToString());
                }
                else
                {
                    //When No Entity is Selected and the Run Button is pressed.
                    MessageBox.Show("Select an Entity First");

                }
            }
            IsRunJobAlreadyExecuted = false;
        }


        void workerforRun_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progress = e.ProgressPercentage;
        }

        void workerforRun_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() =>
            {
                /*When Header is checked and when the Run is clicked, you want to Uncheck the header back after completion.
            This property is set in the CS file.*/
                AllResults = GetJobConfigurationResults();
                foreach (var item in AllResults)
                {
                    item.ExtractIsSelected = false;
                    item.TransformIsSelected = false;
                    item.LoadIsSelected = false;
                }
                SaveTaskSelection();
                JobEntitiesCollectionViewSource.Source = AllResults;
                JobEntitiesCollectionViewSource.View.Refresh();
                ExecuteFilteredEntitiesStoredProcedure();
                IsValidateEnabled = AllResults.Any(p => p.ExtractStatus == "Completed Successfully");
            }), DispatcherPriority.Background, null);
            MessageBox.Show(progress.ToString());
            IsValidationsComplete = true;
        }


CS file:

    public IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
        {
            if (depObj != null)
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
                {
                    var child = VisualTreeHelper.GetChild(depObj, i);
                    // If there is a child found and if the child is of the T type. 
                    //Dont remove null check . If no check i
                    if (child != null && child is T)
                    {
                        yield return (T)child;
                    }
                    foreach (T childOfChild in FindVisualChildren<T>(child))
                    {
                        yield return childOfChild;
                    }
                }
            }
        }

private IEnumerable<CheckBox> GetAllCheckBoxs()
        {
            var allCheckBoxes = FindVisualChildren<CheckBox>(UserControlJob);
            return allCheckBoxes;
        }

private void ChkHeaderExtract_OnChecked(object sender, RoutedEventArgs e)
        {
                var extractCheckBoxes = GetAllCheckBoxs();
                var i = 0;
                foreach (var chk in extractCheckBoxes)
                {
                    if (i%3 == 0) // Since I have many sets of checkboxes and I want only the first set of checkboxes checked when header is clicked Im doing this.
                    {
                        chk.IsChecked = true;
                    }
                    i++;
                }

            }

0 个答案:

没有答案