Thread from Dll
I need to create a dll which starts a timer.
This is easy enough, i'll just use _beginthread, increment a counter for each loop, and Sleep for one second.
The problem is, however, that i need to start multiple instances of this timer, each being able to report back to the single-threaded caller app for each tick.
The caller also have to be able to stop the timer.
The only return from beginthread, is the handle to the thread.
So you have a begin thread function, which starts a timer thread and returns a handle.
When the app wants to stop a timer, it calls a stop timer function and passes the handle to thread in question. This stop timer function must be thread safe.
Presumably you want your thread to notify your application when a timer runs out. You could do this by having the DLL call a callback function in your application. You could pass the address of the callback to your DLL in the start timer function.
However, your callback function must be thread safe because it will be called by the DLL outside of the application's main thread.
Personally, unless there is good reason for using threads, I would implement it using poll function(s), which would be called by the application main thread at regular intervals. This would avoid the need for multiple threads & callback functions, but it may not be suitable for what your trying to do.
Do you really need multiple instances of the timer?
Could you not refer all timing queries to the same "timer"?
I guess i could do with a single thread, but then i would need to dynamically increase/decrease the loopcounters. I don't know how accurate this would be either. I.e. I don't know how long time it takes to perform a switch/if statement, and incrementing values in integer vars..
I'll have to think more thoroughly about this..
This dll is going to be an extension of a VFP program. The creator of the program says that it is going too slow using VFP timers, hence the c++ dll..
>I don't know how long time it takes to perform a switch/if statement, and incrementing values in integer vars..
I'll think you'll find this irrelevant when compared to the fact your timer ticks are only going to be accurate to a millisecond or so.