简体   繁体   English

如何在两个任务之间设置延迟?

[英]How to put a delay between 2 tasks?

I would like to put a delay between 2 actions , let the others tasks (that are between the 2 actions) occurs during this delay but the code will wait until the delay is over to process the second action. 我想在两个动作之间放置一个延迟,让其他任务(在两个动作之间)在此延迟期间发生,但是代码将等待,直到延迟结束以处理第二个动作。

For example like this: 例如这样:

Stopwatch sw = new Stopwatch();

public void 
{
If (checkbox1.checked)
{
   random1.class1();    //action 1
   random2.class2();   //action 2
   sw.start();
}
If (checkbox2.checked)
{
   Random2.class1();    //action 3
   Random2.class2();   //action 4
   sw.stop();
}
 MessageBox.Show(sw.Elapsed.TotalMilliseconds.ToString());
}

Conditions probably takes times to be checked(can't be neglected though) 条件可能需要花费时间进行检查(尽管不能忽略)

I want the most precise delay i could have between action 2 & 4 . 我希望动作2和动作4之间可以有最精确的延迟。 I tried with System.Threading.Thread.Sleep(10); 我尝试使用System.Threading.Thread.Sleep(10); but it makes just stop the code for 10 ms but after it takes times to check the condition and even more to do action3. 但是它会使代码停止10毫秒,但要花一些时间检查条件,甚至需要更多时间才能执行操作3。 I check it with stopwatch and the delay between actions 2 & 4 is fluctuating too much. 我用stopwatch检查了一下,而动作2和动作4之间的延迟波动太大。

I want to put a delay between action 2 & 4 for example 100ms, the code will first check the condition then do action 3 and wait until my delay is over and once the delay s over it ll do action 4. Like that I ll have a precise delay between my 2 methods. 我想在动作2和动作4之间设置一个延迟,例如100ms,代码将首先检查条件,然后执行动作3,并等待直到我的延迟结束,并且一旦延迟结束,它就会执行动作4。我的两种方法之间的精确延迟。 How could I do that?! 我该怎么办?

The aptly-named Task.Delay is for inserting a delay between tasks. 恰当命名的Task.Delay用于在任务之间插入延迟。 As you note, you want an asynchronous wait so that the thread can keep doing work, so remember to await the delay task. 如您所述,您需要异步等待,以便线程可以继续工作,因此请记住await延迟任务。

Note that the delay is not very high precision. 注意延迟不是很高的精度。 Windows is not a real-time operating system, as the other answer correctly notes. Windows不是一个实时操作系统,正如其他答案正确指出的那样。

You could Asynchronous your actions to start in sequence or in a particular order by adding: 您可以通过添加以下内容来异步操作以按顺序或按特定顺序启动:

using System.Threading.Tasks;

And then: 接着:

Task Action1()
{
    // do stuff
    return Task.FromResult(true)
}

Task Action2()
{
    // do more stuff
    return Task.FromResult(true)
}

async void DoInOrder()
{
    await Action1(); // waits for Action1 to finish
    await Action2(); // waits for Action2 to finish
}

Or you could use it for the actual purpose you are delaying between both, if they are directly dependent, like this : 或者,也可以将其用于延迟两者的实际目的(如果它们直接相关),如下所示:

Task<int> GetMyInt()
{
    int value = 3;
    return Task.FromResult(value);
}

async void DoWithMyInt()
{
    int SomeInt = await GetMyInt(); // will wait for GetMyInt()
                                    // to return then assign value
                                    // to SomeInt and then continue
}

You will never get precisse delays at the granularity you want (you're taking in acount things like the time to check the condition which presumabily will be just nanoseconds, so I assume you need a precission of nanoseconds or microseconds). 您永远不会以您想要的粒度获得精确的延迟(您需要花费一些时间来检查条件,例如大概只有几纳秒的时间,所以我假设您需要一个十亿分之一秒或毫秒的精度。

Windows is not an RTOS, so you can't ensure the exact timming for your app at these levels. Windows不是RTOS,因此您不能在这些级别上确保您的应用程序的准确时机。

EDIT: 编辑:

Acording to what you said, something near 10ms can be acceptable, depending on your computer it can be 15ms as the time resolution of windows can have these variations. 根据您所说的,大约10ms的时间是可以接受的,因为您的计算机可能是15ms,因为窗口的时间分辨率可能会有这些变化。

You can use something like this: 您可以使用如下形式:

Stopwatch sw = new Stopwatch();

public void 
{
    If (checkbox1.checked)
    {
       random1.class1();    //action 1
       random2.class2();   //action 2
       sw.start();
    }

    If (checkbox2.checked)
    {
        Random2.class1();    //action 3
        var timeToSleep = (int)Math.Max(100 - sw.ElapsedMilliseconds, 0);
        Thread.Sleep(timeToSleep);
        sw.stop(); //You stop the watch here, you're measuring the time from task2 to task4, not the time which action 4 takes to execute
        Random2.class2();   //action 4

    }

    MessageBox.Show(sw.Elapsed.TotalMilliseconds.ToString());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM