如何等待任务完成 Task.WhenAll ?
How to wait for Tasks to finish with Task.WhenAll ?
到目前为止一切正常,但有两件事困扰着我,我认为我使用了错误的方式
问题 #1 - 我只想 return 一个字符串,但我似乎被迫 return 一个字符串 ARRAY。我怎样才能使这个 return 成为一个字符串?
Task<string> taskVelocifyData = GetVelocifyData();
string[] userEmail = await Task.WhenAll(taskVelocifyData);
问题 #2 - 不确定这是否是一个问题,但这是等待异步任务完成再转到下一个异步任务的最佳选择吗?
private async void DispatcherTimer_Tick(object sender, object e)
{
List<string>[] photos = new List<string>[10];
try
{
//Use Velocify API to get most recent lead
Task<string> taskVelocifyData = GetVelocifyData();
string[] userEmail = await Task.WhenAll(taskVelocifyData);
//Ignore recent lead if it has not changed
if (lsi.VelocifyLeadTitle != previousVelocifyLeadTitle)
{
//If there is an email, use FullContact API to get photos
if (userEmail[0] != "")
{
var taskFullContactData = GetFullContactData(userEmail[0]);
photos = await Task.WhenAll(taskFullContactData);
if (photos.Count() != 0)
{
var taskFaceData = GetFaceData(photos);
}
}
else
{
lsi.FullContactPictures = new ObservableCollection<string>();
}
// DEBUG
// dispatcherTimer.Stop();
LeadSpeakerItems.Add(lsi);
SpeakData(leadSpeakerItems.Last().VelocifyLeadTitle);
}
previousVelocifyLeadTitle = lsi.VelocifyLeadTitle;
}
catch (Exception ex)
{
}
你有一个Task
,所以你可以直接await
。你不需要 Task.WhenAll
.
string userEmail = await GetVelocifyData();
Creates a task that will complete when all of the System.Threading.Tasks.Task objects in an enumerable collection have completed.
完成后,任务将 return 一个包含所有任务结果的数组传递给 Task.WhenAll
。
到目前为止一切正常,但有两件事困扰着我,我认为我使用了错误的方式
问题 #1 - 我只想 return 一个字符串,但我似乎被迫 return 一个字符串 ARRAY。我怎样才能使这个 return 成为一个字符串?
Task<string> taskVelocifyData = GetVelocifyData();
string[] userEmail = await Task.WhenAll(taskVelocifyData);
问题 #2 - 不确定这是否是一个问题,但这是等待异步任务完成再转到下一个异步任务的最佳选择吗?
private async void DispatcherTimer_Tick(object sender, object e)
{
List<string>[] photos = new List<string>[10];
try
{
//Use Velocify API to get most recent lead
Task<string> taskVelocifyData = GetVelocifyData();
string[] userEmail = await Task.WhenAll(taskVelocifyData);
//Ignore recent lead if it has not changed
if (lsi.VelocifyLeadTitle != previousVelocifyLeadTitle)
{
//If there is an email, use FullContact API to get photos
if (userEmail[0] != "")
{
var taskFullContactData = GetFullContactData(userEmail[0]);
photos = await Task.WhenAll(taskFullContactData);
if (photos.Count() != 0)
{
var taskFaceData = GetFaceData(photos);
}
}
else
{
lsi.FullContactPictures = new ObservableCollection<string>();
}
// DEBUG
// dispatcherTimer.Stop();
LeadSpeakerItems.Add(lsi);
SpeakData(leadSpeakerItems.Last().VelocifyLeadTitle);
}
previousVelocifyLeadTitle = lsi.VelocifyLeadTitle;
}
catch (Exception ex)
{
}
你有一个Task
,所以你可以直接await
。你不需要 Task.WhenAll
.
string userEmail = await GetVelocifyData();
Creates a task that will complete when all of the System.Threading.Tasks.Task objects in an enumerable collection have completed.
完成后,任务将 return 一个包含所有任务结果的数组传递给 Task.WhenAll
。