- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
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();
}
}
}
这是一个控制台应用程序,因此线程池用于延续。输出:
Main thread: 11
after Yield(), thread: 7
after Delay(0), thread: 7
after Delay(100), thread: 6
最佳答案
在 Task.Delay
中,它看起来像这样(单个参数 (int)
版本只是调用以下版本):
[__DynamicallyInvokable]
public static Task Delay(int millisecondsDelay, CancellationToken cancellationToken)
{
if (millisecondsDelay < -1)
{
throw new ArgumentOutOfRangeException("millisecondsDelay", Environment.GetResourceString("Task_Delay_InvalidMillisecondsDelay"));
}
if (cancellationToken.IsCancellationRequested)
{
return FromCancellation(cancellationToken);
}
if (millisecondsDelay == 0)
{
return CompletedTask;
}
DelayPromise state = new DelayPromise(cancellationToken);
if (cancellationToken.CanBeCanceled)
{
state.Registration = cancellationToken.InternalRegisterWithoutEC(delegate (object state) {
((DelayPromise) state).Complete();
}, state);
}
if (millisecondsDelay != -1)
{
state.Timer = new Timer(delegate (object state) {
((DelayPromise) state).Complete();
}, state, millisecondsDelay, -1);
state.Timer.KeepRootedWhileScheduled();
}
return state;
}
正如您希望看到的那样:
if (millisecondsDelay == 0)
{
return CompletedTask;
}
这意味着它总是返回一个已完成的任务,因此您的代码将始终继续运行到特定的 await
行。
关于c# - Task.Yield() 与 Task.Delay(0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18527149/
asyncio.sleep() 的阻塞表亲,time.sleep() , 不能保证它会在请求的时间内休眠。 The actual suspension time may be less than th
在 C# 中,我有以下两个简单示例: [Test] public void TestWait() { var t = Task.Factory.StartNew(() => {
这是一个 linqpad 示例,展示了在短暂延迟后异步执行方法的两种方法。这两个例子似乎做了完全相同的事情。我通常会实现第一个版本(使用 Task.Delay.ContinueWith),但我也看到使
这是一个 linqpad 示例,展示了在短暂延迟后异步执行方法的两种方法。这两个例子似乎做了完全相同的事情。我通常会实现第一个版本(使用 Task.Delay.ContinueWith),但我也看到使
使用 jQuery,我逐渐将“go”类添加到 div div { tranform:translate(20px,20px); opacity:0;
期待: 当 audioplayer.play(atTime: 1) 被调用时,定时器重置为 0,音频播放器在第 1 秒播放 现实: 我试过 delay = 0.000000001, 1, 100000
我一直在阅读 .Net 4.5 中引入的 async/await 语法并对其有所了解。 我找到了简单的示例,其中 Task.Yield 是要走的路,而不是 Application.DoEvents。我
我需要在一个延迟很大的系统中调整PI(D)的增益。这是一个常见的温度 Controller ,但温度探头距离加热器较远。一些进一步的信息: 加热器响应上的任何变化,探头的响应都会延迟约10秒 温度以1
代码很短,但是很经典,单位是秒,就是未测试过服务器资源消耗怎么样: <% Function Delay(s)
为什么这会立即清空文本(忽略延迟)? $('#error_box_text').html('error text').delay(5000).html('') # jQuery 1.4 最佳答案 de
我有一个 $image,我 .fadeIn 和 .fadeOut,然后 .remove .fadeOut 完成。这是我的代码: $image .fadeIn() .fadeOut(func
我希望用户将鼠标悬停在对撞机(只是一个透明矩形)上超过 2 或 3 秒时能够看到描述菜单。我目前正在使用 javaFX 扩展。 我将程序设置为每当我将鼠标悬停在对撞机上时,它都会播放声音效果,但我希望
在我创建的几个 .HTA 脚本中,我需要 VBScript WScript.Sleep 命令,它只是等待几毫秒而不使用 CPU。当我浏览网页时,似乎我不是唯一一个在寻找这个的人: https://ww
我在 javaFX 中创建了一些节点。我需要为每个节点设置动画,以便使用 TranslateTransition 在 Pane 中的某个点到某个点之间进行转换。但我需要使每个节点使用不同的起点独立移动
我需要在用户开始触摸屏幕、移动手指然后结束触摸时执行一些操作。触摸开始工作正常,也可以移动,但触摸结束运行有 0.5-1 秒的延迟。下面有一段代码: -(id) init { if (s
我正在寻找有关 jQuery 的 .delay 函数的帮助。我的网站上有一个弹出表单,我想在 2-3 分钟内随机弹出该表单。我了解如何使用 Math.random(),但我不了解如何使用它生成 120
我无法获取一些 javascript 和 Jquery 来延迟适当的时间。我想更改一些文本,请等待 5 秒钟,然后弹出警报。 代码如下: $('#result').html("Record has p
我有以下 JavaScript 片段: $("#dashboard").addClass("standby").delay(3000).removeClass("standby"); $(".acti
我有以下 JavaScript 片段: $("#dashboard").addClass("standby").delay(3000).removeClass("standby"); $(".acti
我已经编写了一个简单的 jQuery 淡入淡出插件,但我无法让延迟正常工作。它将在第一个项目上起作用,但之后它会被忽略 jQuery (function ($) { $.fn.setupQuo
我是一名优秀的程序员,十分优秀!