Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Thread won't sleep?

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

like image 696
Royi Namir Avatar asked Jul 03 '12 09:07

Royi Namir


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


2 Answers

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.

like image 92
Marc Gravell Avatar answered Oct 04 '22 20:10

Marc Gravell


Every second you're starting new thread with 3 sec delay. It happens like this:

  1. thread 1 start
  2. thread 2 start, thread 1 sleeps
  3. thread 3 start, thread 2 sleeps, thread 1 sleeps
  4. thread 4 start, thread 3 sleeps, thread 2 sleeps, thread 1 sleeps
  5. thread 5 start, thread 4 sleeps, thread 3 sleeps, thread 2 sleeps, thread 1 dumps
  6. thread 6 start, thread 5 sleeps, thread 4 sleeps, thread 3 sleeps, thread 2 dumps
  7. thread 7 start, thread 6 sleeps, thread 5 sleeps, thread 4 sleeps, thread 3 dumps

As you can see, each thread sleeps for 3 seconds, yet a dump occurs every second.

How do one works with threads? smth like this:

void Main() {     new Thread(() => doWork()).Start();     Console.ReadLine(); }  public void doWork() {     int h = 0;     do     {         Thread.Sleep(3000);         h.Dump();         h++;     }while(true); } 
like image 45
Agent_L Avatar answered Oct 04 '22 20:10

Agent_L