我有以下代码来打开 SQLite 数据库。当我试图在没有后台工作人员的情况下打开它时,一切都很棒。问题是,它加载了大量数据,所以我想把它扔到后台。
_worker = new BackgroundWorker();
_worker.DoWork += ((s, x) =>
{
_citiesList = new ObservableCollection<City>();
_citiesList = (Application.Current as App).db.SelectCityCollectionCollection(selectString);
});
_worker.RunWorkerCompleted += ((s, x) =>
{
LocationClearText.Visibility = Visibility.Visible;
SearchCityPromptList.ItemsSource = _citiesList.Select(p => p).Where(p => p.cityName.StartsWith(CitySearchBox.Text) || p.flatCityName.StartsWith(CitySearchBox.Text)).Take(30);
});
_worker.RunWorkerAsync();
引发的错误是“无法打开数据库文件:myDatabase.sqlite”。
就像我说的,一切正常,直到我尝试在 BackgroundWorker 中打开它。我应该如何更改我的代码以使其正常工作?