R
R
Roman Rakzin2016-10-21 21:55:41
C++ / C#
Roman Rakzin, 2016-10-21 21:55:41

How in C# to execute a method in the "queue" once a second, and not more often?

I need, as soon as a certain event "Data has been updated" occurs - run the X method - "Dispatch updated data to clients".
But if the event is called, say, 50 times per second, there is no need to immediately run the X method that many times, creating a load.
Is it possible to somehow queue an event, and if there is a queue, run the X method, then execute only the most recent event?
For example, the 5th client connected. And now the 6th client connects immediately, that is, I have to immediately send the updated table of users of five clients (you can, of course, send that a certain client has connected, but now I want to send the entire table of clients online). According to the usual logic, a lot of messages will be sent, but if you make it so that the distribution method is blocked for sending, say for a second, as soon as it is completed and has a run counter. That is, for the next second, 20 attempts to send data came, but he is silent and then sends the actual data
. Option 1: There are 50 clients online. The 51st is connected. I send a 50+1 client a list of online clients. Connect 52nd - do the same.
Option 2: There are 50 clients online. The 51st is connected. I send everything. But then a couple of dozen people connect and I have to send everything out in the same way.
I want, if there has just been a distribution, not to send immediately, but to wait, say, a second and then, if there was a request to send the data, then send the table to the current time. Thus, all clients received data - albeit not instantly, but a second later, but I did not send very many times.
Well, something like this.
Maybe this is of course nonsense, it just became interesting how to implement this?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
Neonoviiwolf, 2016-10-22
@Neonoviiwolf

Delov, there is such a thing - a timer. If someone connects, messages are sent out and a timer starts until the timer finishes counting no new messages can be sent.

M
Maxim, 2016-10-22
@Got_Oxidus

Here
is my idea:

  1. Make It Yourself Method
  2. Run it on a Thread
  3. In this method write Thread.Sleep(1000)//1sec

P
picolino, 2016-10-23
@picolino


Read about the 4th point, maybe it will be useful to you.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question