I’m studying Windows 8 programming and I’ve been fighting with this issue for few hours before getting it working at least somehow. This function seems to be working now somehow. But it sure doesn’t look nice.
Function does login and if login succees then it should spin off task which updates merchants. Execution should not wait for merchant list to complete.
Tricky part is those LoadingMerchants properties which also fire OnPropertyChanged and those needs to be executed in UI thread.
public async Task<bool?> LoginAsync(string username, string password)
{
var uri = new Uri(string.Format("{0}/User/Login?userName={1}&password={2}", ServerURL, username, password));
bool? result = await GetJsonAsync<bool?>(uri);
if(result.HasValue && result.Value)
{
LoginDone = true;
LoadingMerchants = true;
var task = Task.Run(async () => { await LoadMerchantsAsync(); });
var uitask = task.ContinueWith((loadtask) => { LoadingMerchants = false; }, TaskScheduler.FromCurrentSynchronizationContext());
}
return result;
}
Is Task.Run proper way to start execution on new tread?
Do I have lots of extra awaits which are not needed?
Task.Runis the correct way to start execution on a thread pool thread. But you don’t need it.This should work just fine: