I try to execute a simple code asynchronously with TPL of c#. But not in a separated thread! I want to work only with the main thread and I want that my WPF Application doesn't freeze.
That's way, I don't use the methods Task.Run or Task.Factory.StartNew (they create a new thread). I do that:
private async void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
var someTask = new Task<List<Person>>(() => CreateList());
await someTask;
ListBoxControl.ItemsSource = someTask.Result;
}
private List<Person> CreateList()
{
var list = new List<Person>();
list.AddRange(Enumerable.Range(1, 1000000).Select(x => new Person(x)));
return list;
}
But after await, it returns never. What am I doing wrong? Thanks!