I have WPF project with DataGrid. I use MVVM pattern. It's part of my VM:
class LibraryViewModel
{
#region Members
//private SimpleLibDBEntities _database;
ObservableCollection<BooksViewModel> _books = new ObservableCollection<BooksViewModel>();
int count = 0;
int sizeOfdb = 1000000;
#endregion
public ObservableCollection<BooksViewModel> Books
{
get
{
return _books;
}
set
{
_books = value;
}
}
public LibraryViewModel()
{
Task task = Task.Factory.StartNew(Generator);
}
private void Generator()
{
for (count = 0; count < sizeOfdb; count++)
{
_books.Add(new BooksViewModel { Book = new BooksSet { Id = count, Title = "Title"+count, Author = "Author", Publisher = "Publisher", Year = 1000, Note = "Note" } });
}
}
It works, but my DataGrid show me only about 50 000 -100 000 elements(random) instead my int sizeOfdb = 1000000 element. Why it works so? How to repair it?(without "Task" everything work fine)
And how I may to use async/await in this example? Something like that? (not work. try to use Dispathcher? )
public LibraryViewModel()
{
GeneratorAsync();
}
private async void GeneratorAsync()
{
await Task.Factory.StartNew(()=>{
for (count = 0; count < sizeOfdb; count++)
{
_books.Add(...);
}
});
}