相关疑难解决方法(0)

setImmediate与nextTick

Node.js版本0.10今天发布并推出 setImmediate.该API的变化文档建议做递归时使用它nextTick调用.

MDN看起来它看起来非常相似process.nextTick.

nextTick应该何时使用,何时使用setImmediate

javascript node.js setimmediate

328
推荐指数
6
解决办法
9万
查看次数

Task.Yield()与Task.Delay(0)

难道Delay(0)总要得到内联?根据我的经验,它确实:

using System;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApplication
{
    class Program
    {
        static async Task Test()
        {
            await Task.Yield();
            Console.WriteLine("after Yield(), thread: {0}", Thread.CurrentThread.ManagedThreadId);
            await Task.Delay(0);
            Console.WriteLine("after Delay(0), thread: {0}", Thread.CurrentThread.ManagedThreadId);
            await Task.Delay(100);
            Console.WriteLine("after Delay(100), thread: {0}", Thread.CurrentThread.ManagedThreadId);
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Main thread: {0}", Thread.CurrentThread.ManagedThreadId);
            Test().Wait();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是一个控制台应用程序,因此线程池用于继续.输出:

Main thread: 11
after Yield(), thread: 7
after Delay(0), thread: 7
after Delay(100), thread: 6
Run Code Online (Sandbox Code Playgroud)

.net c# multithreading task-parallel-library async-await

14
推荐指数
2
解决办法
6581
查看次数

异步方法阻塞在未等待的任务上

在我当前的项目中,我有一段代码,在将其简化为我遇到问题的地方后,它看起来像这样:

private async Task RunAsync(CancellationToken cancel)
{
    bool finished = false;
    while (!cancel.IsCancellationRequested && !finished)
        finished = await FakeTask();
}

private Task<bool> FakeTask()
{
    return Task.FromResult(false);
}
Run Code Online (Sandbox Code Playgroud)

如果我不等待就使用此代码,无论如何我最终都会阻塞:

// example 1
var task = RunAsync(cancel); // Code blocks here...
... // Other code that could run while RunAsync is doing its thing, but is forced to wait
await task;

// example 2
var task = RunAsync(cancelSource.Token); // Code blocks here...
cancelSource.Cancel(); // Never called
Run Code Online (Sandbox Code Playgroud)

在实际项目中,我实际上并没有使用 FakeTask,通常会有一些 Task.Delay 在那里等待,所以大部分时间代码实际上并没有阻塞,或者只是有限的迭代次数.

然而,在单元测试中,我使用了一个模拟对象,它几乎可以完成 FakeTask …

.net c# asynchronous async-await

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