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 9133269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:27:26+00:00 2026-06-17T08:27:26+00:00

I need to run hundreds thousands of functions at a predefined time, in an

  • 0

I need to run hundreds thousands of functions at a predefined time, in an efficient way,

The code i currently have is like this:

class myclass
{
    public DateTime NextTime = DateTime.Now;
    Random rand = new Random();

    public void DoStuff()
    {
        if (NeedToWork())
        {
            // do some complex stuff on a 2nd thread.
            NextTime = DateTime.Now.AddSeconds(rand.Next(60, 3600));
        }
    }

    public bool NeedToWork()
    {
        return DateTime.Now > NextTime;
    }

}

the calling function that is run from a timer:

    static List<myclass> mylist = new List<myclass>();
    static void Activator()
    {
        foreach (var item in mylist)
        {
            item.DoStuff();
        }

    }

My problem is when there is allot of items in the collection, going through all of them takes a very long time, resulting in some DoStuff() functions running late by over a minute in some cases.

Currently the “Activator” function is called from different threads concurrently to make the delay time as low as possible, (necessary thread synchronization is taken care of by using a Mutex)

The 2 solutions I thought of:

  1. instead of having one List<myclass>, I could have a dictionary like Dictionary<DateTime, List<myclass>>, with 1 second precision, and the each second, run the appropriate class objects, the dictionay would be mapping ‘nexttime’ to ‘myclass’ instances.
  2. create two List<>s or Queue<>s instead of the one list, they would be named ‘fastqueue’, and ‘slowqueue’ , slowqueue would have all the objects, fastqueue would have all the items that are soon to need work, and then have a dedicated thread looping through slow queue, and check the remaining time and put it in fastqueue.

Notes:

  1. The real code doesn’t have any random data determining the next run time, it is actually based on some calculation, this is only a sample.

  2. Each one item doesn’t need more than a fraction of a second to run, and each one run only a maximum of 4 times in a single hour. ram and cpu power is not an issue, I have tested and made allot of optimizations in different areas to make it fit, although not all of code is displayed here.

  3. The only thing that is wasting cpu time is the line that says return DateTime.Now > NextTime
  • 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-06-17T08:27:26+00:00Added an answer on June 17, 2026 at 8:27 am

    You might want to maintain a sorted queue or tree of work, sorted by the time to run.

    Then your regular interval timer loop runs the first N items from the queue, stopping when an item is beyond the current interval time, as there is no need to look further.

    When generating new work to add to the queue, using a sorted data structure, insertion should place it properly to keep it sorted.

    (You’ll also need to worry about thread safety in adding and removing from the queue.)

    (FYI, There are lots of variations on scheduling algorithms.)


    As another note, though not sure if it is useful for you, but when using these kind of time-based formulas, you might consider taking a snapshot of DateTime.Now to use several times, otherwise you can “loose” time if this thread is interrupted between calls to DateTime.Now

    public void DoStuff(DateTime now)
    {
        if (NeedToWork(now))
        {
            // do some complex stuff on a 2nd thread.
            NextTime = now.AddSeconds(rand.Next(60, 3600));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of integers, which could run into the hundreds of thousands
i need run code that will create a database and populate tables. i am
I need to run the application that I have made in Xcode on my
I need to run a script and have access to the default stdin (terminal
I need to run an external application from within my Java code. I can
I'm currently in the need of a simple and efficient thread pool implementation. I
I currently have a script that takes 1,000 rows at a time from a
I have tens of thousands of *.wav files spread out across hundreds of folders.
I have some large files (hundreds of MB) that I need to search for
I have a data set that looks a lot like this: TrxDate DayOfWk TrxHour

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.