Как отправить несколько запросов на сервер с интервалом в несколько миллисекунд в С#C#

Место общения программистов C#
Anonymous
Как отправить несколько запросов на сервер с интервалом в несколько миллисекунд в С#

Сообщение Anonymous »


I have a code that looks like this:

Код: Выделить всё

public static async Task SendRequest(Client client, DateTime startTime, int offset)
{
TimeSpan timeToWait = startTime - DateTime.UtcNow + TimeSpan.FromMilliseconds(offset);
await Task.Delay(timeToWait);
Console.Write(offset);
Request request = client.sendRequest();
}

async static public Task SendMultiple(Client client, DateTime startTime)
{
var offsets = new[] { -15, -10 };
offsets.ForEach(offset => tasks.Add(SendRequest(client, startTime, offset));
Request[] result = await Task.WhenAll(tasks);
}
The problem it seems, is that the prints show that:

Код: Выделить всё

08:59:59,985: -15
09:00:00,015: -10
So the first try was indeed, 15 milliseconds before 9AM. However, the second tried 15 milliseconds after 9AM instead of 10 milliseconds prior.
How could I ensure that those requests are being send exactly at the time I want, please?
I am using Ubuntu*


Источник: https://stackoverflow.com/questions/781 ... val-in-c-s

Вернуться в «C#»