Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 307893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:36:01+00:00 2026-05-12T07:36:01+00:00

I’ve been racking my brain trying to figure this out. Here is the scenario.

  • 0

I’ve been racking my brain trying to figure this out. Here is the scenario. I essentially have a sorted static list that contains various times that an event is supposed to happen. For visualization:

+-----------------------+
|  Time  |  LastUpdate  |
|-----------------------|
|    1   |   03:10:00   | 0
|    2   |   03:10:00   | 1
|    2   |   03:10:00   | 2
|    3   |   03:10:00   | 3
|    3   |   03:10:00   | 4
|    4   |   03:10:00   | 5
+-----------------------+

So, the first time through the method, the lastTime property is going to be null, so it will “do some work” and set the lastTime property to the current time. The time property signifies when the item will need to be executed again. For instance, since element 0 has a lastTime of 03:10:00 and a Time of 1, it will need to be executed at 03:11:00, element 1 and 2 both have a lastTime of 03:10:00 and both need to be executed at 03:12:00, and so and so forth.

Here is a rough implementation of what I have going:

public static IList<Item> _list;

public void DoSomething()
{
    while (true)
    {
        for (int i = 0; i < _list.Count; i++)
        {
            var item = new Item();

            if (DateTime.MinValue.Equals(_list[i].LastUpdate))
            {
                item = DoWork(_list[i].Url);
                _list[i].LastUpdate = item.LastUpdate;
                Console.WriteLine(item.Title + " @ " + item.LastUpdate + "; i = " + i);
            }
            else
            {
                var timeToSleep = ((_list[i].LastUpdate.AddMinutes(_list[i].Time)).Subtract(DateTime.Now));

                if (timeToSleep.TotalMilliseconds > 0)
                {
                    for (int j = 0; j < i; j++)
                    {
                        var lastRet = _list[j].LastUpdate.AddMinutes(_list[j].Time);
                        var nextFetch = DateTime.Now.Add(timeToSleep);

                        if (lastRet < nextFetch)
                        {
                            item = DoWork(_list[i].Url);
                            _list[i].LastUpdate = item.LastUpdate;
                            Console.WriteLine(item.Title + " @ " + item.LastUpdate + "; i = " + i);
                        }
                    }
                }

                if (timeToSleep.TotalMilliseconds > 0)
                {
                    Console.WriteLine("Sleeping until: " + DateTime.Now.Add(timeToSleep));
                    System.Threading.Thread.Sleep(timeToSleep);
                }

                item = DoWork(_list[i].Url);
                _list[i].LastUpdate = item.LastUpdate;
                Console.WriteLine(item.Title + " @ " + item.LastUpdate + "; i = " + i);
            }
        }

        Console.WriteLine("--------------------------");
    }
}

If there is nothing that needs to be done, it will sleep until the next item in the list is ready to be updated. The inner for loop is put in place to prevent the more frequently updated items from having to wait until the less frequently items are updated before it can update itself again. In an ideal scenario, it will check to see if any items above it need updating before it calls Sleep. If any items above the current item will need updating before the the current item sleeps, then go ahead and update them. If not, then the current item will go call Sleep to wait until it is ready to be updated. I hope this makes sense.

Am I going about this totally wrong? Is there an easier solution to this? I am open to any and all suggestions. Also, please keep in mind that this list could potentially grow to thousands of items. Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T07:36:01+00:00Added an answer on May 12, 2026 at 7:36 am

    I don’t entirely understand your problem description, but this seems unnecessarily complicated to me. How about:

    public static IList<Item> _list;
    
    public void DoSomething()
    {
        while (true)
        {
            DateTime minDate = DateTime.MaxValue;
    
            for (int i = 0; i < _list.Count; i++)
            {
                DateTime nextExecution = _list[i].LastUpdate.AddMinutes(_list[i].Time);
    
                if (nextExecution <= DateTime.Now)
                {
                    var item = DoWork(_list[i].Url);
                    _list[i].LastUpdate = item.LastUpdate;
                    nextExecution = _list[i].LastUpdate.AddMinutes(_list[i].Time);
                    Console.WriteLine(item.Title + " @ " + item.LastUpdate + "; i = " + i);
                }
    
                if (nextExecution < minDate)
                    minDate = nextExecution;
            }
    
            TimeSpan timeToSleep = minDate.Subtract(DateTime.Now));
    
            if (timeToSleep.TotalMilliseconds > 0)
            {
                Console.WriteLine("Sleeping until: " + minDate);
                System.Threading.Thread.Sleep(timeToSleep);
            }
        }
    }
    

    If the number of tasks becomes large, you might want to keep a linked list that is ordered by the next calculated execution time. That way, you don’t have to loop through the entire list every iteration.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.