我不知道我做错了什么或者我在Async库中发现了一个错误,但在使用continueWith()回到Synchronized上下文后运行一些异步代码时我遇到了一个问题.
更新:代码现在运行
using System;
using System.ComponentModel;
using System.Net.Http;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
internal static class Program
{
[STAThread]
private static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
MainFrameController controller = new MainFrameController(this);
//First async call without continueWith
controller.DoWork();
//Second async call with continueWith
controller.DoAsyncWork();
}
public void Callback(Task<HttpResponseMessage> task)
{
Console.Write(task.Result); //IT WORKS
MainFrameController controller =
new MainFrameController(this);
//third async call
controller.DoWork(); //IT WILL …Run Code Online (Sandbox Code Playgroud) 在中使用await时forEach,代码不会等待,并且会在之后继续执行代码forEach,但是,在使用normal时for,它将正确等待,请使用以下代码段:
Future<void> uploadTexts(List<String> someTexts) async {
for(int i = 0; i < someTexts.length ; i++) {
var text = someTexts[i];
if (text != null) {
await uploadText(text);
}
}
print("THIS PRINTS AFTER THE FOR IS DONE");
}
Run Code Online (Sandbox Code Playgroud)
Future<void> uploadTexts(List<String> someTexts) async {
someTexts.where((text) => text != null).forEach((text) async{
await uploadText(text);
});
print("THIS PRINTS BEFORE THE FOREACH IS DONE");
}
Run Code Online (Sandbox Code Playgroud)
for结束后将打印第一个示例,而for结束前将打印第二个示例。
难道我做错了什么?