Questions tagged [thread-sleep]

The .net Thread.Sleep(...) method suspends the caller for a specified amount of time.

A call to Thread.Sleep(n) has no side effects, and it will not return until at least n milliseconds have elapsed.

References

815 questions
6
votes
3 answers

Handler postDelayed and Thread.sleep()

I have a thread.sleep and a handler postDelayed in my code: handler.postDelayed(new Runnable() { @Override public void run() { Log.e(TAG, "I ran"); mIsDisconnect = false; } }, DISCONNECT_DELAY); After the handler code…
Jace
  • 439
  • 1
  • 7
  • 19
6
votes
1 answer

Java Thread.sleep() implementation

Can someone help me understand how the Thread.sleep() function is implemented? A thread resumes/wakes up when the specified time elapses or when some other thread interrupts. I'm interested in understanding the design pattern behind the working of…
Praneeth
  • 309
  • 4
  • 14
6
votes
3 answers

sleep-until in c#

I want to run a function periodically every 1 second, so after 10 seconds it is executed 10 times. The simplest approach is using a loop like this : while(true) { Thread.Sleep(1000); function(); } But the main problem with this approach is that it…
user1654052
  • 85
  • 2
  • 7
6
votes
5 answers

Thread.Sleep alternative in Java

I've been told that using Thread.Sleep() is a bad solution at times that one would want to make some time interval within a loop of actions in a synchronized method. On the other hand, I have two different threads which are active throughout the…
Afflatus
  • 933
  • 1
  • 12
  • 39
5
votes
6 answers

Timer vs While Loop - Memory Usages

I am programming a type of game in C# and I want to know what would be a better approach for memory usage. Right now I have it so it goes through a while loop and while the game is running it will check for certain things (is this person dead, etc),…
Mike S
  • 97
  • 1
  • 8
5
votes
2 answers

UnitTesting a threaded class, avoiding Thread.Sleep() in test?

I'm trying to figure out the best way to unit test this class: public class FileGroupGarbageCollector { private Task _task; private readonly AutoResetEvent _event = new AutoResetEvent(false); public void Start() { _task =…
adamwtiko
  • 2,865
  • 8
  • 39
  • 47
5
votes
3 answers

Is it sane to use Thread.Sleep(int) in ASP.NET or should I use another method?

I want to introduce a slight wait during some testing functions, to simulate a server call. Is it sane to use Thread.Sleep(int) to introduce the wait or is there a better method to have the server wait? Note, I'll be pausing long enough for me to…
jcolebrand
  • 15,889
  • 12
  • 75
  • 121
5
votes
4 answers

Would looping Thread.Sleep() be bad for performance when used to pause a thread?

There is (or there has been) a lot of talk about wether it's good or bad to use the Thread.Sleep() method. From what I understand it is mainly to be used for debugging purposes. Now I wonder: is it bad to use for my specific purpose, that is,…
Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
5
votes
2 answers

Does Sleep block program exit? How is it implemented?

In a single-threaded console application, people often use Thread.Sleep as a lazy way to pause the application for some time. This question raised interesting points about NOT using this approach: Why is Thread.Sleep so harmful However other than…
Mr. Boy
  • 60,845
  • 93
  • 320
  • 589
5
votes
7 answers

while loop or Thread.sleep()?

I'm programming a game in Java and I limit the FPS to 60. I figured out 2 different ways to get the same result, but I'm wondering which of them is the better/cleaner way to do it. Or maybe you have a different idea. while(System.nanoTime() -…
Dominik Fuchs
  • 53
  • 1
  • 1
  • 5
5
votes
1 answer

Dispatch rule of concurrent queue using GCD when sleep in one thread

The test code is as followed: dispatch_queue_t queue = dispatch_queue_create("sc", DISPATCH_QUEUE_CONCURRENT); dispatch_async(queue, ^{ [NSThread sleepForTimeInterval:10]; NSLog(@"10s --- %@", [NSThread…
5
votes
0 answers

Write/Read packets generating garbage

I have a client on Android that uses the TUN interface exposed by the Android VPNService in order to intercept packages. In that regard I read all incoming packets and write them to the TUN interface. This basically means that I do a write for each…
Simon Langhoff
  • 1,395
  • 3
  • 18
  • 28
5
votes
2 answers

JFrame getHeight() and getWidth() return 0

I'm making a simple pong game; and part of the collision mechanics require getting the width and height of the canvas to redirect the ball. However, getWidth() and getHeight() return 0 for some reason. Here's the main block of code. package…
Googly_
  • 71
  • 1
  • 7
5
votes
3 answers

Different ways of calling sleep method in threads

I am a beginner to threading. I dont know exactly what is the difference between the three different types of way the thread object has called the sleep method. Also can you please clarify in which type of cases there is a limitation on using the…
roshan dhb
  • 63
  • 1
  • 6
5
votes
1 answer

Android Thread.sleep() in AsyncTask freeze UI

i'm tried many suggestions but nothing works! When i call Thread.sleep() in background thread, main thread also freezes for this time (Animation frame drop) :( Version 1: public void UpdateChannels(final ArrayList channels) { new…
Evgeny E
  • 145
  • 2
  • 11