Thursday, March 1, 2012

DispatcherUnhandledExceptionEventArgs with DataView RowFilter

If you are performing a RowFilter operation from a mulithreaded application on a property that is bound to a grid in the view in you application, you might get an unhandled exception.

You can handle the exception manually with the DispatcherUnhandledExceptionEventArgs.

What happens is that while performing the RowFilter from a separate thread than the main one, if it is quick enough to update the property before the thread has completed, it will throw an exception.

A better place to perform the rowfilter is after the thread has completed. Using the background worker, you can perform the filtering in the RunWorkerCompleted()

        Private _ItemsTable As DataView = Nothing
        Public Property ItemsTable As DataView
            Get
                Return _ItemsTable            End Get
            Set(ByVal value As DataView)
                _ItemsTable = value
                OnPropertyChanged("ItemsTable")            End Set
        End Property

 Private Sub PickThread_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles PickThread.DoWork

                ItemsTable = connString.FillDataTable("SPName", Nothing).DefaultView                       
   
        End Sub

        Private Sub PickThread_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles PickThread.RunWorkerCompleted            

                If Not ItemsTable Is Nothing OrElse Not ItemsTable.Count < 1 Then                   
                            ItemsTable.RowFilter = " Pick = 'T' "                       
                End If

        End Sub

No comments:

Post a Comment