Doomulation said:
Does that mean that maybe it wouldn't deadlock if called from a diffrent thread?
It means it wouldn't deadlock if you gave your process a chance to call GetMessage(...). Whether by the means of using separate threads or whatever other way you can devise.
What could be an example of bad things when I terminate a thread?
The "bad things" come down to the fact that you can't predict where in the thread it'll stop.
Consider three lines of operation that happen in a thread:
Code:
PerformTask1();
PerformTask2();
PerformTask3();
If you used TerminateThread(...) to stop the thread, you wouldn't know which of the three tasks, if any, were actually performed. It doesn't come down to whether one task was performed or not either. Each of those function calls will be implemented using lots of discreet CPU instructions. The thread could get terminated 23% into PerformTask1(), 99.99% into PerformTask3(), or even before PerformTask1().
Consider if in those functions, memory allocation and freeing takes place. Stopping the thread at incorrect places can cause memory leaks. (Not that much of a big deal with small programs in our clever modern OSes.)
Getting a little advanced... If you're going to be using threads to their full extent, you'll be worrying about atomicity, memory sharing, critical sections, et al. One common way of sharing resource between multiple threads is to lock it while one thread is using it so that other threads can't touch it during this time and wreck its integrity. Windows provides EnterCriticalSection(...)/LeaveCriticalSection(...) for basic locking.
What would happen if a thread is terminated after it calls EnterCriticalSection(..), to work with a resource, but before it finishes with LeaveCriticalSection()? (The lock will be lost and other threads won't be able to access the resource.) What would happen the thread is terminated 23% into EnterCriticalSection(...)? (It's undocumented.. but you could end up with some "half locked" lock, and this could be more hazardous than just losing the lock.)
Also, according to the windows sdk, handles to threads must be closed before the threads are entirely removed from the system.
Yes, the memory used by a thread is not entirely freed until the thread's handle is closed. That is what CloseHandle(hCounterThread) is for.
If you interpreted it to mean that you need to close the thread BEFORE you can terminate or exit it, then that's an error in your interpretation. If you have doubts, cite the part of the documentation in question so I know where you're coming from.
It seems awfully stupid to me that you have to dispatch a message when the timer should call a callback function.
I'm not sure of the specifics, so I don't know if it's DispatchMessage() that calls the callback or not, but I do know this: There is no timer. --In the sense you're thinking. The OS doesn't use some background thread to keep track of your timer, to put a message on your queue when the time comes. It's all done by your own application. --In a way, I assume, that is very similar to the method I posted couple posts back with GetTickCount(). This is probably done in GetMessage(...). GetMessage(), when it's called, basically looks through your list of active "timers" and check if it's been n + the time when the timer was set. If so, it puts a message on its own queue or calls the callback (or flags DispatchMessage() to do that).
speaking of callback functions, is it possible to send a callback to a class function as callback?
Non-static class methods can't be used as callback functions. (Because there's no THIS pointer)