D
D
Daniel2021-02-16 08:38:00
C++ / C#
Daniel, 2021-02-16 08:38:00

C# How to delay the time of the thread or start the Task function in Nanoseconds or even less?

The task is, here is a code that I roughly got up

bool isFirst=true;
        /// Приходит значения на порты асинхронно, или еще хз.., запустить функцию обработки значений на них,
        /// Значение может прийти на 1 порт, или на оба, если на оба, то нужно только 1 раз их обработать 
        /// их свежие значения. 
        public void OnChangePort(Port p,int index)
        {
            if (index == 0)
            {
                Port1.SetValue(p.GetValue());
            }
            else
            {
                Port2.SetValue(p.GetValue());
            }
            if (isFirst) // если еще не была запущенна функция Propagate(которая может запускаться только по  частоте.
            {
                long tiks = 3; // ; 
                ///????? Как здаержать на число тактов или ПикоСекунд.
                System.Threading.Thread.Sleep((tiks)); /// ждем пока прийдет значение на второй порт через пару тактов
                /// елси не пришло   то и фиг с ним. 
                Propagate(); // выполнить работу с портами(как бы работает по частоте типа 3ГигаГерцю
            } 
               

        }
        public void Propagate()
        {
            Work(Port1.GetValue(), Port2.GetValue(), Operation); // выполнить операцию
            isFirst = true;  /// разрешить дальше  обрабатывать значения
        }

Answer the question

In order to leave comments, you need to log in

3 answer(s)
F
freeExec, 2021-02-16
@daniil14056

///????? How to delay by the number of ticks or PicoSeconds.

Windows is not a real-time operating system. You cannot control ticks. Have you ever encountered hanging applications? Management can be returned in a minute, even if you ordered just a little bit.

V
Vasily Bannikov, 2021-02-16
@vabka

It is forbidden. When working with the network, use an asynchronous approach and timeouts. Nanoseconds are delays at the RAM bus level, not at the network level. It won't work.

S
soloveid, 2021-02-16
@soloveid

The smallest you can delay is 15ms.
On server machines, this value is even higher.
These restrictions are introduced because switching execution between threads is not an instantaneous operation.
Of course, you can have all your threads do Thread.Yeild() and they will give their time to other threads,
but most likely this will not help your thread.
In addition to the task itself and the code:
- usually they read from the threads and add them somewhere, and then the thresher thread processes the values, so you can separate your logic, and not write the entire application in the code for receiving data from sockets.
- your isFirst variable will not meet expectations, if you work with threads, then you cannot work with shared variables this way.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question