How to use DispatcherHelper with ProgressBar and BackgroundWorker

May 2, 2013 at 4:43 PM
How can I use Dispatcher Helper with Progress Bar in Mvvm. I'm accessing Ui Element defined in my viewmodel and I think dispatcherhelper will solved this, but it seems the progressbar was not updated when I ran the Backgroundworker with DispatcherHelper. Please help:

here's my code :

_worker = new BackgroundWorker() {
        WorkerReportsProgress = true,
        WorkerSupportsCancellation = true
    };


    _grid = new Grid();
    _worker.DoWork += (sender,e) => DispatcherHelper.UIDispatcher.Invoke(() => {
        try{
            var vWorker = (BackgroundWorker) sender;

            ReportColumns = new List<ReportColumn>();
            #region Set Report Item Column
            for(var i = 1;i <= _reportBal.ReportItemContent.Columns.Count - 1;i++) {
                var vColumnsItems = ReportParser.ParseColumnAttributes(_reportBal.ReportItemContent.Columns[i].ColumnName);
                var vColumn = new ReportColumn();
                vColumn.DataType = vColumnsItems[0];
                vColumn.Width = vColumnsItems[1];
                vColumn.Header = vColumnsItems[2];
                vColumn.DataField = vColumnsItems[2];
                ReportColumns.Add(vColumn);
            }

            if(ReportColumns != null) {
                for(int ctrCol = 0;ctrCol <= ReportColumns.Count - 1;ctrCol++) {
                    var sp = new StackPanel { Orientation = Orientation.Horizontal };
                    _grid.ColumnDefinitions.Add(new ColumnDefinition {
                        Width =
                            new GridLength(Convert.ToDouble(ReportColumns[ctrCol].Width))
                    });
                    var tb = new TextBlock {
                        Text = ReportColumns[ctrCol].DataField,
                        FontWeight = FontWeights.DemiBold,
                        Margin = new Thickness(8,0,8,0),
                        TextWrapping = TextWrapping.Wrap
                    };
                    sp.Children.Add(tb);
                    Grid.SetRow(sp,0);
                    Grid.SetColumn(sp,ctrCol);
                    _grid.Children.Add(sp);
                }
            }

            #endregion
_recordCount = _reportBal.ReportItemContent.Rows.Count;
            #region Set Report Item Row
            GridHelper.SetRowCount(_grid,_reportBal.ReportItemContent.Rows.Count + 1);
            for(int rRow = 0;rRow <= _reportBal.ReportItemContent.Rows.Count - 1;rRow++) {

                if(vWorker.CancellationPending) {
                    e.Cancel = true;
                    break;
                }

                for(int cCol = 0;cCol <= ReportColumns.Count - 1;cCol++) {

                    string vValue = _reportBal.ReportItemContent.Rows[rRow][cCol + 1].ToString(); ;

                    var sp = new StackPanel { Orientation = Orientation.Horizontal };

                    var tb = new TextBlock {
                        Text = vValue,
                        Width = double.Parse(ReportColumns[cCol].Width),
                        Margin = new Thickness(8,3,8,3),
                        TextWrapping = TextWrapping.Wrap
                    };

                    sp.Children.Add(tb);

                    Grid.SetColumn(sp,cCol);
                    Grid.SetRow(sp,rRow + 1);
                    _grid.Children.Add(sp);


                }

                if(vWorker.WorkerReportsProgress) {

                    int vPercentComplete = (int)((float)rRow / (float)_recordCount * 100);
                    string updateMessage =
                        string.Format("Record(s) {0} of {1}",rRow,_recordCount);
                    vWorker.ReportProgress(vPercentComplete,updateMessage);
                }
            }

            #endregion

            e.Result = _grid;
            //_reportItemContent = _grid;
        }
        catch(Exception exception) {
            throw new Exception("#Error: Error parsing with report items" + Environment.NewLine +
                Environment.NewLine + "#Internal Error: " + exception.Message,exception);

        }
    });
    _worker.ProgressChanged += (s,e) => DispatcherHelper.RunAsync((() =>{
        ProgressPercentage = e.ProgressPercentage;
        Output = (string)e.UserState;
    }));

    _worker.RunWorkerCompleted += (s,e) => /*DispatcherHelper.UIDispatcher.Invoke((() =>*/ {
        if(e.Error != null) throw new Exception("Error Arg");
        _reportItemContent = (Grid)e.Result;
    };