Programming Forums
User Name Password Register

RSS Feed

Thread Tools Display Modes
Old Oct 25th, 2008, 5:11 PM   #1
Join Date: Oct 2008
Posts: 1
Rep Power: 0 Farrukhw is on a distinguished road
Red face vb6 timer control

I'm trying to update label's caption on a splash screen form with multiple messages e.g. Initializing...., Loading..., finishing.... I'm using timer control, but the problem is, the Timer1_Timer() event triggers after the end of calling procedure. Like

Private Sub Command2_Click()
Frm_Splash.UpdateLable "Initializing..", 2000 '2000 is the interval value to Timer control
Frm_Splash.UpdateLable "Loading..", 3000
Frm_Splash.UpdateLable "Finishing..", 1000

end sub

but the problem here is that only Finishing message is triggered by Timer control, because Timer triggered after the end sub of Command2_click.

Can anyone please guide?

Thanks in advance.
Farrukhw is offline   Reply With Quote
Old Oct 27th, 2008, 5:30 AM   #2
Join Date: Oct 2008
Posts: 2
Rep Power: 0 mbshinde78 is on a distinguished road
Re: vb6 timer control


I found this code on another forum. Credit goes to that forum poster.

Public Declare Function GetTickCount Lib "kernel32" () As Long

Public Sub wait(ByVal dblMilliseconds As Double)
    Dim dblStart As Double
    Dim dblEnd As Double
    Dim dblTickCount As Double
    dblTickCount = GetTickCount()
    dblStart = GetTickCount()
    dblEnd = GetTickCount + dblMilliseconds
    dblTickCount = GetTickCount()
    Loop Until dblTickCount > dblEnd Or dblTickCount < dblStart
End Sub

You declare it in global module and call Wait function with interval. Remove timer control and the code in timer.
mbshinde78 is offline   Reply With Quote
Old Nov 22nd, 2008, 9:48 PM   #3
Professional Programmer
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 446
Rep Power: 13 mackenga is on a distinguished road
Re: vb6 timer control

Ah, this is a common problem - VB won't process the Timer event until it gets back to the event loop. This fact is a bit invisible in VB, but basically until your program lets VB sit idle it can't see events. It looks like what's happening in your example is that you set the timer in the call, but your UpdateLable routine returns immediately having done that.

Basically to do this sort of thing with VB you've got two choices: either do it the hard way, and have the repeated timer events trigger the ongoing work in little increments (e.g. use a static / member variable to remember which 'state' you're in between Timer calls and do a different part of the startup progress each time); or, you can do it the easy way, something like:

vb Syntax (Toggle Plain Text)
  1. lblProgress.Caption = "Starting up ..."
  2. DoEvents
  3. ' do some work here
  4. lblProgress.Caption = "Loading configuration ..."
  5. DoEvents
  6. ' load the configuration here
  7. lblProgress.Caption = "Doing something else ..."
  8. DoEvents
  9. ' More work here

And so on. DoEvents lets VB process pending events, which means your label update will happen. It can be a nuisance sometimes if other events are processed as well though.

Oh, almost forgot to add: the delay code in mbshinde78's post has one flaw: it uses a lot of CPU time while it's waiting (known as a 'busy wait'). It'll be worthwhile for you if you look into the Win32 API 'Sleep' call and how to use it from VB; lots of examples online. This does a 'proper' wait, actually suspending the process, although it does have the downside that it doesn't DoEvents in the meantime so your application appears to 'hang'. You can combine the 'busy wait' type loop with Sleep calls to do the sleeping in small increments (e.g. Sleep 125 to sleep for 1/8th of second at a time) though and DoEvents between them, and it still saves the machine quite a bit of work.
"There is no absolute knowledge, and those who claim it, whether they are scientists or dogmatists, open the door to tragedy" -- Jacob Bronowski
mackenga is offline   Reply With Quote


« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Show web user control hidden see07 C# 4 Aug 15th, 2010 10:44 PM
Saving image from picture control darkies22 C++ 1 Jan 31st, 2008 6:53 AM
VB6 - Mouse Wheel / Scroll NOT WORK on Datagrid control ransomjulian Visual Basic 0 Nov 9th, 2007 8:14 PM
How to add inherited control to toolbox. InfoGeek C# 3 Feb 8th, 2007 10:12 PM
Computer Based Chess Timer ZeeMan Other Programming Languages 6 Oct 29th, 2006 10:43 PM

DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:52 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC