Question

I have a LongListSelector which load transaction data into it. I have no problem with data loading. I want to filter data for a date period and I know how do it with linq. The problem is the process runs in a backgroundWorker and I cant get the value from DatePicker as it violate cross thread operation.

My current code with in background worker is

Private Sub bw_loadTraDoWork(sender As Object, e As DoWorkEventArgs)
    'all transactions
    Try
        Dispatcher.BeginInvoke(Sub()
                                   SetProgress(True, "Working...")
                               End Sub)
        Dim db = New n_DataContext(Common.ConnectionString)
        Dim query = (From t In db.TransactionTable Join c In db.ExCat On c.ID Equals t.cat_id Order By t.tra_date Descending Order By t.ID Descending Select New TraList With {.tra_type = c.n_type, .amount = t.amount, .tra_date = t.tra_date, .note = t.note, .cat_name = c.cat_name, .ID = t.ID})

        Dim groupedObjects = From myObject In query Group myObject By myObject.tra_date Into grouped = Group Select New Grouping(Of DateTime, TraList)(grouped)

        Dispatcher.BeginInvoke(Sub()
                                   LstTransactions.ItemsSource = groupedObjects
                               End Sub)
    Catch ex As Exception
        MessageBox.Show("An error occured. try later.", "Oops!", MessageBoxButton.OK)
    End Try
End Sub

I tried with wrap a variable in Dispacher.BeginInvoke but can not get the value.

Please help me with this.

Was it helpful?

Solution

When you invoke RunWorkerAsync method pass a parameter in this way:

worker.RunWorkerAsync(pickerData)

Then in DoWork sub-routine get the value you just passed:

Dim pickerData As CustomObject = DirectCast(e.Argument, CutomObject)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top