小编Max*_*aga的帖子

在ContinueWith()之后,ConfigureAwait(False)不会更改上下文

我不知道我做错了什么或者我在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)

.net c# asynchronous async-await

3
推荐指数
1
解决办法
2277
查看次数

list.forEach在异步方法中未正确等待

在中使用awaitforEach,代码不会等待,并且会在之后继续执行代码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结束前将打印第二个示例。

难道我做错了什么?

async-await dart flutter

2
推荐指数
1
解决办法
60
查看次数

标签 统计

async-await ×2

.net ×1

asynchronous ×1

c# ×1

dart ×1

flutter ×1