Is working with a timer the right tool for this job?

Suppose you have an application that allows users to control all their pitchers. In the end, all milk jugs expire.

I want to control all jugs of milk to make sure their isExpired property must be true or false.

Do I have to go through each jug every time with something like a timerjob to update the properties? What if I want instant feedback after the jug has expired? What tools are available to me in the .NET / C # world to make this inexpensive / efficient?

Ultimately, I will use something like signalR for push notifications for web clients after the jug expires. I just don’t know how to determine WHEN the jug expired without repeating everything constantly.

==

Forgive me for typing two questions, but it’s obviously only important to send notifications when a user watches pitchers. So, maybe a timer works that only works with an active user session? Any tips / recommended readings here would be great.

+5
source share
3 answers

If you want instant notification when the pitcher expires, you can do the following:

Scan the list of jugs and find the one with the earliest expiration date
Set a timer to expire at that date/time.
When the timer expires, remove that jug from the list.
Scan the list of jugs to find any other jugs that expire at this time.
Send notifications for all the expired jugs.
Go to step 1.

There are several problems with this algorithm:

  • If you add a jug, you should check to see if it expires before the current expiration time. If so, you should reset the timer for the current duration of the pitcher.
  • , , , , . , reset .
  • , , .

, . , . , . , , .

( , , ) . , , . , .

, , . .

, .NET:

TimeSpan expirationDelay = jug.ExpirationDateTime - DateTime.Now;

System.Threading.Timer jugTimer = new System.Threading.Timer(
    JugExpirationCallback, null, expirationDelay, TimeSpan.FromMilliseconds(-1));

, , . , Change.

, expirationDelay . , DateTime UTC . WaitableTimer . WaitableTimer .

, , . , , ( ), : . , , , . , , , .

, . , , . , . .

, , : , , . reset .

, " ". , .

+4

sortedlist, , (, , , 1). (30 ?), .

Also, the tip does not update the properties, but it has the timedate expirationDate property asking for "Secondary Remaining" or something you need to update that poses problems. (Sorry your comments about updating properties bothered me.) As for the actual expired property, just make the get action check the expiration date, don't worry about setting it up.

Well, he can check the active session yes.

0
source

All Articles