There is a list of tasks. Denoted as a task and runtime. Time is relative. That is, if it is indicated that the first task should be launched on January 1, 2016 13: 50: 10,456, and the second one on January 1, 2016 13: 50: 10,496, then the first task will be started at the current time, and the second should start after 40 ms.

I already broke my head how to do it.

It is clear that each task must be run in a separate thread. The problem is that creating a stream also takes time, and the intervals between tasks can be 1 ms. In this case, I can not afford to lose even 1 ms.

There was an idea to start the threads in advance, and loop the loop in them (or turn on the timer) to start the task, but the task list can have tens of thousands of tasks.

  • 2
    Where did you get such wonderful demands about 10,000 tasks and the fact that even 1ms cannot be lost? Given that the thread does not guarantee the execution time. Maybe you even need a real-time OS? - Kromster
  • 2
    Without a real-time system this can not be done. Conventional OS never give you the desired accuracy, without errors. - ixSci
  • one
    Well, great, start the flow for the task for 1000ms with a margin, and then start the body in a stream with a more precise timer. - Kromster
  • 2
    The guarantee of a call at a certain moment can only be given to you by RTOS. With an ordinary system, there are no guarantees, your stream can be hibernating for at least half an hour. Those who wrote TZ are not very good specialists. - VladD
  • one
    @VladD, I remembered a video about 7 red lines. But what to do, I'm an expert, I have to do))) - iRumba

2 answers 2

I am making a summary of my clarifications from the comments

Where did you get such wonderful demands about 10,000 tasks and the fact that even 1ms cannot be lost? Given that the thread does not guarantee the execution time. Maybe you even need a real-time OS?

All 10,000 tasks in the threads do not need to be kept. In theory, there is a manager, he can create threads for tasks, for example, 1-2 seconds before the start. You will not have more than a dozen simultaneous tasks?

Well, great, start the flow for the task for 1000ms with a margin, and then start the body in a stream with a more precise timer.

    It takes less than a millisecond to start a new thread, so there is no point so early, in 1-2 seconds to start. To synchronize the start of the task, you can use ManualResetEventSlim , it has minimal costs, i.e. the thread starts, and calls Wait() , the thread manager calls Set() at the right time. Stopwatch suitable for timing, and as a timer, you can call in a Thread.Sleep(0) loop. Yes, the thread does not interfere with the maximum priority set. Only the garbage collector will still spoil the picture periodically.