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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:20:04+00:00 2026-05-17T22:20:04+00:00

How would you limit the number of operations per second? Lets say we have

  • 0

How would you limit the number of operations per second?

Lets say we have to copy files from one location to another and we don’t want more than 5 files to be processed per second.

Please see what I am doing

private static string currentStamp;
private static int processedInCurrentStamp = 0;

private static void Main(string[] args)
{
    currentStamp = DateTime.Now.ToString("{0:d/M/yyyy HH:mm:ss}");
    Run();
}

private static void Run()
{
    for (int i = 0; i < Int32.MaxValue; i++)
    {
        string s = DateTime.Now.ToString("{0:d/M/yyyy HH:mm:ss}");
        if (currentStamp.Equals(s))
        {
            if (processedInCurrentStamp < 5)
            {
                ProcessItem();
                processedInCurrentStamp++;
            }
        }
        else
        {
            Console.WriteLine("{0} ::: {1}", currentStamp, processedInCurrentStamp);
            currentStamp = s;
            processedInCurrentStamp = 0;
        }
    }
}

But I need a more elegant and bullet proof way.

  • 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-17T22:20:04+00:00Added an answer on May 17, 2026 at 10:20 pm

    Get the starting time, and then in the loop calculate the maximum number of files that should be processed up to the current time, and sleep if you are ahead:

    DateTime start = DateTime.UtcNow;
    
    int i = 1;
    while (i <= 100) {
    
      int limit = (int)((DateTime.UtcNow - start).TotalSeconds * 5.0);
    
      if (i <= limit) {
    
        Console.WriteLine(i);
        i++;
    
      } else {
        Thread.Sleep(100);
      }
    
    }
    

    This way the code will catch up if some operations take longer. If you only get three operations one second, it can do seven the next second.

    Note that I am using UtcNow instead of Now to avoid the nasty jump in time that happens twice a year.

    Edit:

    Another alternative is to measure the time that an operation takes, and sleep the rest of it’s time slot:

    for (int i = 1; i <= 100; i++ ) {
    
      DateTime start = DateTime.UtcNow;
    
      Console.WriteLine(i);
    
      int left = (int)(start.AddSeconds(1.0 / 5.0) - DateTime.UtcNow).TotalMilliseconds;
      if (left > 0) {
        Thread.Sleep(left);
      }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a class, I would like to limit the number of objects created from
As per Yahoo, we have to limit number of request to the server's to
I have two text fields that I would like to limit the number and
I'd like to write a decorator that would limit the number of times a
I have a DTD and I would like to limit a few attribute properties'
I have a text field whose length I would like to limit at the
I have implemented a TTPickerTextField in my application, I would like to limit the
In Linux, limit on the number of inotify instances a process can have open
I am developing a node.js service that will have a number of requests per
I am Writing a Android calculator and would like to limit the number of

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.