Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
535
update the datasource of xamPivotGrid in another thread or task
posted

Hi,

I try to set the datasource of the xamPivotGrid in another thread to invoid the UI blocking when loading data.

I tried this and doesn't work. can you help me to resolve this.

public partial class MainWindow : Window
{


public OrderLineFlatDataSource Datasource { get; set; }

//public static TaskScheduler Context = TaskScheduler.FromCurrentSynchronizationContext();
public MainWindow()
{
InitializeComponent();

}


private void Window_Loaded(object sender, RoutedEventArgs e)
{
progress.Visibility = Visibility.Visible;
progress.IsBusy = true;
//LoadData();
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += (ss, ee) =>
{
OrderLineFlatDataSource fds = new OrderLineFlatDataSource();
fds.Initialize();
ee.Result = fds;
};

worker.RunWorkerCompleted += (ss, ee) =>
{
Datasource = ee.Result as OrderLineFlatDataSource;
progress.Visibility = Visibility.Collapsed;
progress.IsBusy = true;

this.xamPivotGrid.DataSource = Datasource;

//-- i have  System.InvalidOperationException: 

// --The calling thread can not access this object because another thread owns it
};

worker.RunWorkerAsync();
}

}

  • 34690
    Offline posted

    Hello Michel,

    I have created a sample project on my end that tests the code that you have provided, and this exception appears to be stemming from the setting of XamPivotGrid.DataSource trying to access a FlatDataConnectionSettings object internally. It appears that this FlatDataConnectionSettings object is created when you set the ItemsSource of the FlatDataSource, which I imagine is likely happening in your "Initialize()" method of your custom FlatDataSource. This is currently called in "DoWork" and so it is happening on a separate thread. This is very likely the reason you are seeing this error.

    The best thing I can recommend in this case is that you set the FlatDataSource.ItemsSource on the UI thread instead of within the Initialize() method - assuming that you are setting it there. Perhaps you could send something else to the e.Result, such as an object array that holds your FlatDataSource as well as the collection that you are setting to the FlatDataSource.ItemsSource property? Then, you could set the ItemsSource of the FlatDataSource in RunWorkerCompleted, which should prevent the exception you are seeing.

    Please let me know if you have any other questions or concerns on this matter.