It's Sleep (capital S).
Printable View
It's Sleep (capital S).
@smasherprog
As i stated above i tried to use Sleep with Createthread but i couldnt manage to close the thread (CloseHandle) once the time elapsed.
Please advise if you got a solution for that problem, thanks.
@rogster001
Its a GUI not a console program so i dont have a game loop.
Yes but CloseHandle isn't meant to stop a thread. CloseHandle is simply your way of saying, I'm no longer interested in what happens with regards to that thread and no longer need any control over it. That's actually counter-productive if your goal is to stop it!
There are several ways to actually make a thread exit when you want to, but fortunately you aren't paying much attention to some of the suggestions as to how that can be done. It's just as well because if you can do it without a thread then you're probably better off doing it without a thread, especially at your level of expertise.
You actually had about the best solution already, before you came here. Use GetTickCount, possibly combined with Sleep if you would otherwise create a busy-loop.
Yes, thank you iMalc for the clarification.
Now im looking into setting a flag on the thread and calling ExitThread.
I would use GetTickCount if i was worried by punctuality but im not and comparing two integers must
be less CPU stressing than calling a function every time.
Avoid using ExitThread. This usually indicates a bad design.
Let the thread do its work and then terminate. Do not signal it to close.
Design your function so that it does its task without needing to terminate! What is so difficult?
i dont mean for console app, i use this method to animate pop up windows, it just means there is no local loop that takes over in an animation function and events can still be monitored, sleep is called just once in the function until that function call is skipped, ie the animation is complete
@rogster
I appreciate your help but without code i dont understand a word you're saying, i think im just not there yet.
@Elysia "What is so difficult?"
Its just me agonising over the GetTickCount() method thinking that calling a function every milliseconds would put unnecessary stress on the CPU. But im probably wrong.
But its ok i found a method to close a thread from outside.
WaitForSingleObject with CreateEvent.
What good will this do? If you need to spawn a new animation or something every millisecond, then using synchronization primitives such as WaitForSingleObject will only slow it down!
I keep repeating this: the thread should do its thing and close. You should not have to tell the thread to close. If that is the case, then the code to manage whatever you are doing is in the wrong place. Don't put it inside the thread.
Example: Animation.
The animation consists of moving a pixel every 2 ms. Then the thread would move pixel, wait 2 ms, move pixels, etc, until the object has moved to the desired position. Then the thread exits.
Now, the main thread, schedules these animations, spawning a thread for each animation, as many as it takes, how often it wants.
No synchronization. No telling the thread to exit.
Am I completely off what you want here?
Well if i shouldnt close the thread from outside then i dont need thread at all because i have a while(1) loop in my function and either way i have to check at every loop if its time to break out or not.
And for that the best and "only?" way would be GetTickCount().
lol Ok you convinced me! :)
I know you just said you've been convinced but for posterity's sake...
GetTickCount() is a trivial call and it's very fast, but let's test it anyway to be sure. Let's time a few batches of GetTickCount() to see how long they take, how about 1 billion calls?
Output on my system:Code:#include <windows.h>
#include <iostream>
using namespace std;
int main()
{
for (int i = 0; i < 5; ++i)
{
DWORD oldtick = GetTickCount();
for (unsigned int ui = 0; ui < 1000000000; ++ui)
{
GetTickCount();
}
cout << "Took " << (GetTickCount() - oldtick) << "ms\n";
}
return 0;
}
Took 4196ms
Took 4118ms
Took 4134ms
Took 4119ms
Took 4134ms
Maybe you think QueryPerformanceCounter() is faster?
Took 30108msCode:#include <Windows.h>
#include <iostream>
using namespace std;
int main()
{
LARGE_INTEGER li;
for (int i = 0; i < 5; ++i)
{
DWORD oldtick = GetTickCount();
for (unsigned int ui = 0; ui < 1000000000; ++ui)
{
QueryPerformanceCounter(&li);
}
cout << "Took " << (GetTickCount() - oldtick) << "ms\n";
}
return 0;
}
Took 30202ms
Took 30155ms
Took 30155ms
Took 30139ms
... yeah.
Thats pretty fast indeed but it makes your CPU works 100%. (50% if you got a dual core)
Now put a Sleep(1); in the loop to avoid this and the performance is gonna drop down significantly.
A 1000 tries take 2 secs.
Can we see the code you're trying to run in a loop?
Well when you're repeatedly telling it to sleep for 1ms, that's going to add up to a lot of time sleeping if you don't do much else. In fact the sleeping alone there has to account for at least 1 second according to how Sleep is documented, and typically more. The fact that your real code will actually be doing something useful will mean that overall it wont be spending quite as much time sleeping.