Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我怎样才能推迟活动?_C#_.net_Async Await_Delay - Fatal编程技术网

C# 我怎样才能推迟活动?

C# 我怎样才能推迟活动?,c#,.net,async-await,delay,C#,.net,Async Await,Delay,我想延迟一条指令的执行,同时不执行任何其他指令。 public async void parcours\u anim(队列f,noeud[]Arb\u b,LabelExpression[]Lbls\u A) { int-val=0; while(f.ToArray().Length!=0) { val=f.Dequeue(); 指数=Recherche_指数(val,Lbls_A); //在执行此指令2秒钟之前,请在此停止 Arbre[index].Fill=brusks.Blue; //然

我想延迟一条指令的执行,同时不执行任何其他指令。

public async void parcours\u anim(队列f,noeud[]Arb\u b,LabelExpression[]Lbls\u A)
{
int-val=0;
while(f.ToArray().Length!=0)
{
val=f.Dequeue();
指数=Recherche_指数(val,Lbls_A);
//在执行此指令2秒钟之前,请在此停止
Arbre[index].Fill=brusks.Blue;
//然后继续
}
}
您可以在
异步方法中使用等待


在您的情况下,您可以添加
wait Task.Delay(2000)
,以便等待2秒,然后继续执行。

如@dotctor所写,您可以使用
Task.Delay
,但是您必须将方法签名更改为
async Task
,而不是
async void
,否则调用方法不会等待此方法完成

请将代码添加为文本,而不是图像线程。Sleep(2000)感谢您的回答,我尝试了该任务。延迟,但问题是它太快了,以至于它一直在执行循环,然后所有标签都同时上色。您是否将
async void parcours\u anim
更改为
async Task parcours\u anim
public async void parcours_anim(Queue<int> f, noeud[] Arb_b, LabelExpression[] Lbls_A)
{
    int val = 0;
    while (f.ToArray().Length != 0)
    {
        val = f.Dequeue();
        index = Recherche_index(val, Lbls_A);
        //Stop here before executing this instruction for like 2seconds
        Arbre[index].Fill = Brushes.Blue;
        // and then continue
    }
}