如何在此示例中使用 ContinueWith
How to use ContinueWith with this example
我有以下异步方法,它应该根据传递给该方法的列表获取我需要的所有字符串:
public async Task<List<string>> GetStrings(List selections)
{
List<string> myList = new List<string>();
TaskCompletionSource<List<string>> someStrings = new TaskCompletionSource<List<string>>();
// Register/deregister event that is fired on each selection.
// Once this event is received, I get its arguments from which I
// get string and add it to my list of strings.
// Once the list count matches number of selections passed into the method,
// TaskCompletionSource result is set.
EventHandler<IDataNotification> onCallThatFiresCompleted = null;
onCallThatFiresCompleted = (sender, e) =>
{
var myString = e.getString();
myList.Add(myString);
if (myList.Count == selections.Count)
{
MyObserver.DataNotification -= onCallThatFiresCompleted;
someStrings.TrySetResult(myList);
}
};
MyObserver.DataNotification += onCallThatFiresCompleted;
foreach (var sel in selections)
{
// This call fires onCallThatFiresCompleted event that when received
// will conteain string I need in its arguments.
CallThatFires_MyEvent();
}
return await someStrings.Task; //await for the task
}
一旦任务完成,此方法的调用者将如何使用 Task.ContinueWith() 来处理返回的列表?我想做这样的事情:
foreach(var s in returnedStringList)
{
// do something with the string s
}
How to use ContinueWith with this example
你不知道。你使用 await
:
var returnedStringList = await GetStrings(..);
foreach(var s in returnedStringList)
{
// do something with the string s
}
正如我在我的博客中所描述的,await
is superior in every way to ContinueWith
用于异步代码。
我有以下异步方法,它应该根据传递给该方法的列表获取我需要的所有字符串:
public async Task<List<string>> GetStrings(List selections)
{
List<string> myList = new List<string>();
TaskCompletionSource<List<string>> someStrings = new TaskCompletionSource<List<string>>();
// Register/deregister event that is fired on each selection.
// Once this event is received, I get its arguments from which I
// get string and add it to my list of strings.
// Once the list count matches number of selections passed into the method,
// TaskCompletionSource result is set.
EventHandler<IDataNotification> onCallThatFiresCompleted = null;
onCallThatFiresCompleted = (sender, e) =>
{
var myString = e.getString();
myList.Add(myString);
if (myList.Count == selections.Count)
{
MyObserver.DataNotification -= onCallThatFiresCompleted;
someStrings.TrySetResult(myList);
}
};
MyObserver.DataNotification += onCallThatFiresCompleted;
foreach (var sel in selections)
{
// This call fires onCallThatFiresCompleted event that when received
// will conteain string I need in its arguments.
CallThatFires_MyEvent();
}
return await someStrings.Task; //await for the task
}
一旦任务完成,此方法的调用者将如何使用 Task.ContinueWith() 来处理返回的列表?我想做这样的事情:
foreach(var s in returnedStringList)
{
// do something with the string s
}
How to use ContinueWith with this example
你不知道。你使用 await
:
var returnedStringList = await GetStrings(..);
foreach(var s in returnedStringList)
{
// do something with the string s
}
正如我在我的博客中所描述的,await
is superior in every way to ContinueWith
用于异步代码。