Solved

Getting the Timer to start

Posted on 2009-04-02
5
224 Views
Last Modified: 2012-05-06
I setup a Timer and start it with all the instructions I know of.  However, it does not go to the Timer procedure until after what I am trying to time has finished.

My code:

PRIVATE SUB
        ' I START THE TIMER
        Timer_Progress.Interval = 1000
        Timer_Progress.Enabled = True
        Timer_Progress.Start()

        ' I TURN ON MY PROGRESS BAR
        ProgressBar1.Value = 0
        ProgressBar1.BackColor = Color.Goldenrod
        ProgressBar1.ForeColor = Color.Gold
        ProgressBar1.Visible = True

       more code
       .
       .    
        Timer_Progress.Enabled = False
END SUB

AS IT CYCLES THRU MY CODE I WANT THE BELOW PROCEDURE TO EXECUTE TO ADVANCE MY PROGRESS BAR, BUT IT ONLY GOES THERE AFTER THE ENTIRE ABOVE CODE HAS FINISHED.

 Private Sub Timer_Progress_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer_Progress.Tick
        ProgressBar1.Value = ProgressBar1.Value + 5
    End Sub

Any ideas?
0
Comment
Question by:GarySB
5 Comments
 
LVL 7

Expert Comment

by:cmrobertson
ID: 24051270
try reordering the timer command to enable it before setting the interval and start
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 24051284
For that you don't need a timer, you only have to do your increments in the sub, like:
ProgressBar1.Value = 0
ProgressBar1.BackColor = Color.Goldenrod
ProgressBar1.ForeColor = Color.Gold
ProgressBar1.Visible = True
'  some code
ProgressBar1.Value  = 10
' other code
ProgressBar1.Value = 20
' more code
ProgressBar1.Value = 30
 
etc
 
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 100 total points
ID: 24051628
"BUT IT ONLY GOES THERE AFTER THE ENTIRE ABOVE CODE HAS FINISHED."

The code is "stuck" in the Sub...you need to call Application.DoEvents() periodically in your code to allow the Tick() events to be processed.

The same would hold true for jpaulino's suggestion.  Call Application.DoEvents() after each update to allow the GUI controls to repaint themselves.

If you have VB.Net 2005 (or above) then use the BackgroundWorker() control instead:
http://msdn.microsoft.com/en-us/library/c8dcext2(vs.80).aspx
0
 
LVL 48

Assisted Solution

by:jpaulino
jpaulino earned 25 total points
ID: 24051758
Forgot the Application.DoEvents() after set the value but you can use that or ProgressBar1.Update()
Thanks for the correction Idle :)
0
 

Author Closing Comment

by:GarySB
ID: 31565848
I had ProgressBar1.Value  = 10, 20 etc., but dealing with Forms copying hung it up.
Appreciate your help.  Reordering Enable command did not change.
Placing  Application.DoEvents() worked, but had to place it all thru code.
I increment 5 and this fills in the remaining gap at the end of procedure
        Do Until ProgressBar1.Value = 100
            Application.DoEvents()
        Loop
Thank You Genius's
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now