Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.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 - Fatal编程技术网

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

C# 如何在两个任务之间设置延迟?,c#,.net,C#,.net,我想在两个操作之间设置一个延迟,让这两个操作之间的其他任务在此延迟期间发生,但代码将等到延迟结束后再处理第二个操作 例如: Stopwatch sw = new Stopwatch(); public void { If (checkbox1.checked) { random1.class1(); //action 1 random2.class2(); //action 2 sw.start(); } If (checkbox2.checked) { R

我想在两个操作之间设置一个延迟,让这两个操作之间的其他任务在此延迟期间发生,但代码将等到延迟结束后再处理第二个操作

例如:

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());
}
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());
}
检查条件可能需要时间,但不能忽视

我希望在行动2和4之间有最精确的延迟。 我尝试了System.Threading.Thread.Sleep10;但这会使代码停止10毫秒,但检查情况需要时间,执行操作需要更多时间3。我用秒表检查,动作2和4之间的延迟波动太大


我想在动作2和4之间设置一个延迟,例如100ms,代码将首先检查条件,然后执行动作3,并等待我的延迟结束,一旦延迟结束,将执行动作4。这样,我将在两种方法之间有一个精确的延迟。我怎么能这么做

你永远不会得到你想要的精度延迟,比如检查条件的时间,大概只有纳秒,所以我假设你需要纳秒或微秒的精度

Windows不是RTOS,因此您无法确保应用程序在这些级别上的精确计时

编辑:

根据你所说的,接近10毫秒的时间是可以接受的,取决于你的电脑,它可以是15毫秒,因为windows的时间分辨率可能有这些变化

您可以使用以下内容:

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());
}
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());
}
名称恰当的Task.Delay用于在任务之间插入延迟。正如您所注意到的,您需要一个异步等待,以便线程能够继续工作,所以请记住等待延迟任务

请注意,延迟的精度不是很高。正如另一个答案正确指出的那样,Windows不是实时操作系统。

您可以通过添加以下内容,按顺序或特定顺序启动操作:

using System.Threading.Tasks;
然后:

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
}
或者,如果它们是直接相关的,您可以将其用于实际目的,在两者之间进行延迟,如下所示:

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
}

您使用的是实时操作系统吗?否则,无法保证第二个操作将在何时执行。使用window无法执行此操作?您最好旋转CPU并不断检查时间。请参阅Thread.SpinWait方法。我实际上想要一个波动大于或小于10 ms的延迟,5 ms将非常大。动作3有20-30毫秒的波动,太大了。延迟100毫秒对我来说是可以的,如果它保持在95到105之间。。。