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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:22:25+00:00 2026-06-06T22:22:25+00:00

I have this code : void Main() { System.Timers.Timer t = new System.Timers.Timer (1000);

  • 0

I have this code :

void Main()
{
    System.Timers.Timer t = new System.Timers.Timer (1000);
    t.Enabled=true;
    t.Elapsed+= (sender, args) =>c();
    Console.ReadLine();

}

int h=0;
public void c()
{
    h++;
    new Thread(() => doWork(h)).Start();
}

public void doWork(int h)
{
    Thread.Sleep(3000);
    h.Dump();
}

I wanted to see what happens if the interval is 1000 ms and the job process is 3000 ms.

However I saw a strange behavior –
the 3000 ms delay occurs only at the start !

How can I make each doWork sleep 3000 ms?

As you can see here, at the beginning there is a 3 second delay, and then it iterates 1 second each.

enter image description here

  • 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-06T22:22:26+00:00Added an answer on June 6, 2026 at 10:22 pm

    Every time the timer ticks, you start a thread to do some sleeping; that thread is completely isolated, and the timer is going to keep on firing every second. Actually, the timer fires every second even if you move the Sleep(3000) into c().

    What you have currently is:

    1000 tick (start thread A)
    2000 tick (start thread B)
    3000 tick (start thread C)
    4000 tick (start thread D, A prints line)
    5000 tick (start thread E, B prints line)
    6000 tick (start thread F, C prints line)
    7000 tick (start thread G, D prints line)
    8000 tick (start thread H, E prints line)
    ...
    

    It is unclear what you are trying to do. You could disable the timer when you don’t want it firing, and resume it again once ready, but it is unclear what the purpose of the Sleep() is here. Another option is just a while loop with a Sleep() in it. Simple, and doesn’t involve lots of threads.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say, I have a code snippet like this: public static void main(String[] args) {
I have this code: hubSpinner.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int
I have this code for an event handler: rbM.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton
I have this JSON response string: {d:{\ID_usuario\:\000130\,\Nombre\:null,\Vipxlo\:0,\Provmun\:null,\Descuentos\:null,\Listaviplocal\:null}}` With this code: - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
I have this code private void writeReport(IReport report, string reportName) { string reportString =
Lets say I have this code: public void MyMethod(string Data, List<string> InputData) { //I
I have this code: @Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { GLES20.glClearColor(0, 0,
i have this code public interface Interfcae1 { void OP1(); } public interface Interfcae2
I have this piece of code: private void myFunc(){ obj = doSomething(); //If value
i have this IntentService code which run a service once: protected void onHandleIntent(Intent intent)

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.