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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:15:49+00:00 2026-05-28T14:15:49+00:00

Possible Duplicate: Run a code in given time interval I have a method that

  • 0

Possible Duplicate:
Run a code in given time interval

I have a method that runs on the hour. I am currently doing this like so:

while (true)
{
        string rightNow = DateTime.Now.ToString("mm");

        if (rightNow == "00")
        {
            RunMyMethod();
        }
        Thread.Sleep(10000);
}

I have been reliably informed by a friend that I’m an idiot (and generally a bad person), and that I should be using events and delegates to do this instead. I get that, outside the scope of any methods I need declare a delegate with the same return type as my method, and with the same input parameters like so:

public delegate void MyMethodsDelegate();

Then, when I want to use my delegate I instantiate it in the same way I instanciate a class like so:

MyMethodsDelegate foobar = new MyMethodsDelegate(RunMyMethod);

Then I can invoke my delegate like so:

foobar.Invoke();

Ok so I have my delegate setup, how about the event? The event I want to create is ‘It’s x o’clock’, then every time it’s x o’clock this kicks off my delegate

Also, am I setting up my delegate correctly?

Thanks

  • 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-28T14:15:50+00:00Added an answer on May 28, 2026 at 2:15 pm

    You can use System.Timers.Timer class which executes a delegate asynchronously and raises the Elapsed event when given time interval is elapsed. I would strongly recommend you reading MSDN remarks part for the Timer class, see here.

    System.Timers.Timer timer = new Timer(60 * 60 * 100);
    

    Case 1: Subscribe to anonymous method

    timer.Elapsed += (s, e) =>
        {
            // callback code here
        };
    

    Case 2: Subscribe to explicitly defined delegate

    ElapsedEventHandler handler = (s, e) =>
        {
            // callback code here
        };
    timer.Elapsed += handler;
    

    Case 3: Subscribe to the existing method

    timer.Elapsed += this.OnHourElapsed;
    private void OnHourElapsed(object sender, EventArgs args)
    {
        // callback code here
    }
    

    Then finally just start a timer

    timer.Start();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Conditional operator cannot cast implicitly? I have run into a peculiar situation
Possible Duplicate: Online PHP IDE In php it is possible to run code and
Possible Duplicate: Floating point comparison When I run the code below I get the
Possible Duplicate: How to run a database script file from Delphi? I have a
Possible Duplicate: Will code in finally run after a redirect? Hello, What happens when
Possible Duplicate: Simple PHP Mailer Script i run a small website and we have
Possible Duplicate: Run Jquery method when enter key is press I am trying to
Possible Duplicate: Python float - str - float weirdness I run the following code
Possible Duplicate: Send email using System.Net.Mail through gmail. (C#) I am using this code
Possible Duplicate: mysql_fetch_array() expects parameter 1 to be resource, boolean given in select Here's

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.