Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can you have a timer without using the control

Status
Not open for further replies.

brailian

Programmer
Oct 11, 2001
11
US
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?)

Thanks,
AJ
 
You can build a timer using the sleep API call.

It sleeps for n milliseconds.

Wil Mead
wmead@optonline.net

 
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?
 
The man has no forms.

Wil Mead
wmead@optonline.net

 
Dim MyTimeIsNow as Double

MyTimeIsNow = Timer 'seconds since a day in 1970

do
Loop While Timer - MyTimeIsNow < 5

'this will make your code hang for 5 seconds
 
My solution to the &quot;control without a form&quot; 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.




Wil Mead
wmead@optonline.net

 
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.

Chip H.
 
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.
 
Pass a 0 for HWND argument. You don't need it.

Chip H.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top