What's the best way to update an ObservableCollection from another thread?

后端 未结 5 976
终归单人心
终归单人心 2020-12-23 17:06

I am using the BackgroundWorker to update an ObservableCollection but it gives this error:

\"This type of CollectionVi

5条回答
  •  情歌与酒
    2020-12-23 17:40

    If you initialize the collection in the constructor it will be on the default Application thread.

    To invoke the main thread you can do this:

    Application.Current.Dispatcher.Invoke((Action)(() =>
        {
           //Do something here.
        }));
    

    You have to cast the Anonymous delegate as an action otherwise it gets confused ¯\O_o/¯

    If you are using the Async CTP then you can do this

    Application.Current.Dispatcher.InvokeAsync(()=>
        {
           //Do something here.
        });
    

提交回复
热议问题