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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:27:31+00:00 2026-05-23T15:27:31+00:00

I have a method send() that I wish to execute/call every 1 second. I’m

  • 0

I have a method send() that I wish to execute/call every 1 second. I’m having difficulty implementing this. So far this is what I have come up with in my main program:

bool done = false;
while (!done)
{
    string vCurrent = RandomVoltage(220, 240) + "" + RandomCurrent(10, 13);
    int seconds = RandomSec();
    if (isEven(seconds))
    send(vCurrent, "169.254.156.135");//send the string to the ip address
}

So basically I try call my send() method for every second of the current time that is even, and I skip the odd seconds, here is how I tried to implement that with my RandomSec() and isEven() methods:

    private static readonly object syncLock = new object();
    public static int RandomSec()
    {
        lock (syncLock)
        { 
            return DateTime.Now.Second;
        }
    }

    public static bool isEven(int sec)
    {
        if ((sec % 2) == 0)
            return true;
        else return false;
    }

Now the problem is when I run the while loop in my program, my send() method sends a big bunch of strings in 1 second, then pauses for 1 second and then sends another big bunch of messages when the current second is even again. How can I get my program to execute my send() method only ONCE every 1 second, so that the send() method sends only 1 string every even second rather than say 20/30 of them. Is it possible for me to call my send() method in a time controlled loop? Any help is greatly appreciated.

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-23T15:27:32+00:00Added an answer on May 23, 2026 at 3:27 pm

    You can use the Timer class.

    Sample Code from the above link:

    public class Timer1
     {
    
         public static void Main()
         {
             System.Timers.Timer aTimer = new System.Timers.Timer();
             aTimer.Elapsed+=new ElapsedEventHandler(OnTimedEvent);
             // Set the Interval to 5 seconds.
             aTimer.Interval=5000;
             aTimer.Enabled=true;
    
             Console.WriteLine("Press \'q\' to quit the sample.");
             while(Console.Read()!='q');
         }
    
         // Specify what you want to happen when the Elapsed event is raised.
         private static void OnTimedEvent(object source, ElapsedEventArgs e)
         {
             Console.WriteLine("Hello World!");
         }
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 5 parameters and I want to send them to the method: public
serial.write() method in pyserial seems to only send string data. I have arrays like
I have method in a class that I need to make sure is only
I have class method that returns a list of employees that I can iterate
I have a method that where I want to redirect the user back to
I have a method in my Python code that returns a tuple - a
I have a method that can return either a single object or a collection
I have this method on a webpart: private IFilterData _filterData = null; [ConnectionConsumer(Filter Data
In my application controller, I have a method that is supposed to check for
If I have the method public void send() { /* some code */ }

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.