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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:56:13+00:00 2026-05-26T04:56:13+00:00

It has to be trivial, but I just cannot get through it. I have

  • 0

It has to be trivial, but I just cannot get through it.
I have to limit amount of tasks (let’s say connections, emails sent or clicks in the button) per amount of time. So e.g. I can send 1000 emails per hour.

How can I do that in c#? I don’t know and don’t care how much time each operation will take. I just want to make sure that for last hour, only 1000 will be executed.

  • 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-26T04:56:14+00:00Added an answer on May 26, 2026 at 4:56 am
     class EventLimiter
     {
        Queue<DateTime> requestTimes;
        int maxRequests;
        TimeSpan timeSpan;
    
        public EventLimiter(int maxRequests, TimeSpan timeSpan)
        {
            this.maxRequests = maxRequests;
            this.timeSpan = timeSpan;
            requestTimes = new Queue<DateTime>(maxRequests);
        }
    
        private void SynchronizeQueue()
        {
            while ((requestTimes.Count > 0) && (requestTimes.Peek().Add(timeSpan) < DateTime.UtcNow))
                requestTimes.Dequeue();
        }
    
        public bool CanRequestNow()
        {
            SynchronizeQueue();
            return requestTimes.Count < maxRequests;
        }
    
        public void EnqueueRequest()
        {
            while (!CanRequestNow())               
                Thread.Sleep(requestTimes.Peek().Add(timeSpan).Subtract(DateTime.UtcNow));
                // Was: System.Threading.Thread.Sleep(1000);
    
            requestTimes.Enqueue(DateTime.UtcNow);
        }
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say file.txt.gz has 2GB, and I want to see last 100 lines or
The answer to this is likely to be trivial, but I have spent half
I have problem that seems to be rather trivial but I was not able
I'm overriding a protected function in a subclass. Let's say I have two classes,
Sorry if this might seem trivial for me to ask but.. I have some
Has anyone used Rhino igloo in a non-trivial project? I am curious if it's
Has anyone looked at Yahoo's ASTRA ? It's fairly nifty, but I had some
Has anyone been able to get an NHibernate-based project up and running on a
I believe this is trivial, but I can't for the life of me, figure
G'day, This has been asked before for VC++ but I am interested in the

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.