3 Replies - 342 Views - Last Post: 20 October 2017 - 10:26 AM Rate Topic: -----

#1 KansaiRobot  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 10-March 11

Task.Delay(2) is not giving me a delay of 2 ms

Posted 18 October 2017 - 10:34 PM

I am having a problem with some code which uses Task.Delay. I am probably using it bad because when I try it to wait 2 ms (1000 times) it waits completely different times

My code is

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

#if !UNITY_EDITOR && UNITY_WSA
using System.Threading.Tasks;
#endif




public class ExampleScript : MonoBehaviour {


    // Use this for initialization

    void Start () {
        Debug.Log("Start():: Starting");
        
#if !UNITY_EDITOR && UNITY_WSA


      //  Task.Run( ()=>SlowJob()   );
        
         Task.Run( async ()=>SlowJob()   ); //Should I put async here?


        //SlowJob();


#endif
        Debug.Log("Start():: Done");
    }
    

    void SlowJob()
    {
        Debug.Log("ExampleScript::SlowJob()  --Doing 1000 things , each taking 2ms");

        System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
        sw.Start();




        for(int i=0;i<1000;i++)
        {


#if !UNITY_EDITOR && UNITY_WSA


        //This will require to declare SlowJob to be async 
         //  await Task.Delay(2);


        //We use a way that is NOT recommended by experts so we have to be aware of this
       // Task.Delay(2).Wait();


       //How about this
            WaitFor2MiliSeconds();
#endif


        }


        sw.Stop();
        Debug.Log("ExampleScript::SlowJob() --Done! Elapsed Time:" + sw.ElapsedMilliseconds / 1000f);


    }



#if !UNITY_EDITOR && UNITY_WSA
     private async void WaitFor2MiliSeconds()
    {
 //        await System.Threading.Tasks.Task.Delay(2);
         await System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(2.0));
    
     }

#endif


}


This is a script for using Unity with a Hololens (UWP)
I am sure I am misunderstanding the use of Delay somehow here
The results are
When I use Task.Delay(2).Wait the code that should take 2 seconds takes 15 seconds

When I use await.Task.Delay() in the WaitFor2MiliSeconds() function the code that should take 2 seconds takes 0.2 seconds if synchronous or 0.4 if asynchronous

What should I modify in order to wait only 2ms (therefore the fucntion SlowJob taking only 2 seconds)

I will greatly appreciate any help here. Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Task.Delay(2) is not giving me a delay of 2 ms

#2 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6384
  • View blog
  • Posts: 25,783
  • Joined: 12-December 12

Re: Task.Delay(2) is not giving me a delay of 2 ms

Posted 19 October 2017 - 01:07 AM

Why don't you try just waiting 2 seconds in total? At least until you get it working.

At least then you would recognise whether attempting an extremely short, and probably inaccurate, time period in a loop is causing an issue.

(If I wanted to test a looped delay, for some reason, I would also start with something more manageable such as 200ms for 10 iterations.)
Was This Post Helpful? 0
  • +
  • -

#3 JapanDave  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 366
  • Joined: 01-February 16

Re: Task.Delay(2) is not giving me a delay of 2 ms

Posted 19 October 2017 - 01:16 AM

I don't understand why you want to delay the task for only 2ms? I am guessing you don't want to block the main thread by using Thread.Sleep, but if you have a slow job, why not just stick that task on a new thread and let it finish when it finishes? I could understand wanting to delay a thread for a time 50ms or 100ms, but 2ms I can't wrap my head around it, it seems to be not worth the trouble???
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5899
  • View blog
  • Posts: 20,142
  • Joined: 05-May 12

Re: Task.Delay(2) is not giving me a delay of 2 ms

Posted 20 October 2017 - 10:26 AM

Looks like our OP is another Intellisense user who doesn't bother to go back and read the documentation:

Quote

This method depends on the system clock. This means that the time delay will approximately equal the resolution of the system clock if the millisecondsDelay argument is less than the resolution of the system clock, which is approximately 15 milliseconds on Windows systems.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1