问题描述:

I have about 5 voids that query the database and then set the datasource of some controls. As the UI was not frozen while updating the controls, I'm using a BackGroundWorker. Is there a way I can use the same voids to update the controls? just calling them from the DoWork... (it crashs when It tries to update the datasources with the void that the DoWork called).

Thanks Allon. Great reply! I need to get better basis for my WPF coding.. :S this is what I did..

private void UpdateStates(Boolean UpdateLeft, Boolean UpdateRight)

{

DataSet ds = new DataSet();

States.State State = new States.State();

if (UpdateRight == true)

{

cState.Dispatcher.Invoke(() =>

{

ds = State.StatesTable();

cState.DataContext = ds.Tables[0].DefaultView;

cState.DisplayMemberPath = ds.Tables[0].Columns[1].ToString();

cState.SelectedValuePath = ds.Tables[0].Columns[0].ToString();

});

}

}

Is this ok? it tells me that it cant convert lambda on system.delegate type because it is not a delegated type.. any ideas? I'm googling to get but you help will be great for me! Thanks!

网友答案:

If you want to interact with a WPF controls from a background thread, you must use their dispatcher to marshal the call to the UI thread. So instead of accessing the control directly:

var data = LoadAHugeDataset()
myControl.DataContext = data;

Use the control's Dispatcher to run the code:

var data = LoadAHugeDataset()
myControl.Dispatcher.Invoke(() => { myControl.DataContext = data; });
相关阅读:
Top