Could I suggest a few changes? After the await
statement in the button click handler you are back on the dispatcher thread so you don’t need the Dispatcher BeginInvoke stuff. It is generally not a good idea to try to martial the threads yourself, you will get in a tangle. Leave all that to the async
method. Also, the way you deal with exceptions is incorrect. Any exception raised in an asynchronous method is only thrown when the data is read at the await
statement. So your try
block should be positioned around the await
statement. It’s best to make the method that you wish to run asynchronously as self contained as possible. I would return a ObservableCollection<hotitem></hotitem>
from it and refractor the method into a standalone method without the try/catch
blocks. So you would end up with something like this.
private ObservableCollection<HotItem> RetrieveHotList()
{
var _hotItems = new ObservableCollection<HotItem>();
return _hotItems;
}
private async Task<ObservableCollection<HotItem>> DownloadHotListAsync()
{
var _hotItems = new ObservableCollection<HotItem>();
try
{
_hotItems = await Task.Run(() => RetrieveHotList());
}
catch (Exception ex)
{
}
return _hotItems;
}