I want to put a timer into a class that has no user interface (i.e. no form to drop the timer object onto). Does anyone know of a simple way to do this? Do I have to use an API call? (if so which?)
Only you may not want to, as this VB is (generally) single-threaded, which means that your application stops processing whilst the sleep is in progress.
I'm with strongm on this one. The sleep function would be okay, but only in a very limited set of circumstances. Just note that you can't even redraw the form while your sleeping, much less process anything else.
Not to take this off topic, but I have a more general version of the question: Can you use ANY control without drawing it on a form? For example, a couple of weeks ago, I tried to build a wrapper class for modem access using the MSComm control. However, I couldn't figure out how to use the control without putting it on a form. Dim and Set just didn't work. Is there a general method for doing this with controls like the Timer and MSComm which normally require a GUI?
My solution to the "control without a form" problem is simply not to solve it. Create an invisible form to hold the controls. I know it's the easy way out. More often then not I end up creating a debug aid that activates with command line parameter anyway.
You can use the SetTimer and KillTimer API functions.
When calling SetTimer you pass in the address of a public subroutine that is located in a .BAS module (using the addressof operator) that gets called when the timer elapses. It returns you a handle to the timer which you use when killing the timer.
No form required, and your process is not blocked.
Heh, unfortunately, SetTimer seems to require an hwnd argument which you only get if you have a form/window, therefore...back to square one. Might as well create a form, drop a timer on it, and load it w/o showing it. Aesthetically repugnant, but that's life. Seems my question has been answered. Thanks to all who responded.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.