I have an array of integers. For every integer, the program needs to wait for that exact amount of time, and then run some special code. The first thing I tried was using a for loop, and every loop would use the Sleep() function for a delay before running the special code. However, that didnt work very well because Sleep doesnt have very good accuracy (Sleep(20) will sleep for at least 40ms in reality). I need the accuracy/granularity to be to the millisecond, preferably better if possible.
The next thing I tried was using timers. For every integer in the array, I would set the timer interval to the value of the integer and when the timer fired/ticked, it would run the special code. This option also doesnt work as the timer suffers from the same inaccuracy as the Sleep function.
After that, I thought I would do away with inbuilt things entirely, and make my own system. I was going to use some sort of infinite while loop that would check against system time with Datetime.Now, which would be far more accurate. Though when I have a while loop it obviously wont let the main thread refresh and it would freeze up. I cant use Sleep to solve this problem, because even Sleep(0) will sleep for at least 20ms longer than it is supposed to. So, I though that if I ran this loop on a different thread, it wouldnt lock up the main thread and everything would be fine.