Progress bar

Simon Scott 306 Reputation points
2022-02-24T16:45:01.147+00:00

Good afternoon,

I'm trying to implement a progress bar which increases every second my Timer is counting down from 300 seconds, however it doesn't seem to work.

Here are some snippets of my code

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer1.Interval = 300000
Timer1.Start()
ProgressBar1.Minimum = 0
ProgressBar1.Maximum = 300
ProgressBar1.Value = 0

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If ProgressBar1.Value <= 300 Then
ProgressBar1.Value = ProgressBar1.Value + 1
Else
ProgressBar1.Value = 0
End If
CountDown()

Private Sub CountDown()
Static OnlyOne As New Object
Static _wait As New Threading.ManualResetEvent(False)
Static stpw As New Stopwatch
Static CntDwnFrm As New TimeSpan(0, 0, 300)
If Threading.Monitor.TryEnter(OnlyOne) Then
stpw.Restart()
Dim t As Task
t = Task.Run(Sub()
Do
Do
Me.BeginInvoke(Sub()
Label3.ForeColor = Color.LightGray
Label3.Text = String.Format("{0:n0}" & " seconds",
(CntDwnFrm - stpw.Elapsed).TotalSeconds)
End Sub)
If stpw.Elapsed >= CntDwnFrm Then
stpw.Restart()
End If
Loop While _wait.WaitOne(1000)
Loop
Threading.Monitor.Exit(OnlyOne)
End Sub)
End If
End Sub

Can anyone point me in the right direction to resolve this please?

I have tried but to no avail!

Thanks
Simon

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,714 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. LesHay 7,126 Reputation points
    2022-02-24T18:22:05.527+00:00

    Hi
    Slight confusion of quite what you want. So, here is some code that will illustrate a ProgressBar being used with progress shown increasing from zero, and, progress shown decreasing from maximum to zero. The example has 2 buttons, one for INCREASING and one for DECREASING. The PBinc variable is used to hold the incremental size (I used 50 to speed up testing, adjust as needed. Give it a try and see if it helps. This is a stand alone example.

    177596-111.png

    Option Strict On  
    Option Explicit On  
    Public Class Form1  
    	Dim PBinc As Integer = 1  
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
    		Timer1.Interval = 1000  
    	End Sub  
    	Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  
    		' decreasing  
    		StartPB(300, False)  
    	End Sub  
    	Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click  
    		' increasing  
    		StartPB(300, True)  
    	End Sub  
    	Sub StartPB(max As Integer, inc As Boolean)  
    		ProgressBar1.Maximum = max  
    		If inc Then  
    			ProgressBar1.Value = 0  
    			PBinc = 50  
    		Else  
    			ProgressBar1.Value = max  
    			PBinc = -50  
    		End If  
    		Timer1.Enabled = True  
    	End Sub  
    	Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick  
    		ProgressBar1.Increment(PBinc)  
    		If ProgressBar1.Value < 1 Or ProgressBar1.Value >= ProgressBar1.Maximum Then  
    			Timer1.Stop()  
    		End If  
    	End Sub  
    End Class  
    
    0 comments No comments

  2. Castorix31 84,546 Reputation points
    2022-02-24T18:37:33.113+00:00

    I'm trying to implement a progress bar which increases every second my Timer is counting down from 300 seconds

    You can use a Thread only, instead of Timer + Thread
    A way =>
    (I set 10 seconds instead of 300 to test)
    (remove space at S leep)

    177528-progressbar-seconds.gif

    Public Class Form1  
      
        WithEvents button1 As System.Windows.Forms.Button  
        WithEvents progressBar1 As System.Windows.Forms.ProgressBar  
      
        Private wt As Threading.Thread = Nothing  
        Private ReadOnly isCanceled As Threading.AutoResetEvent = New Threading.AutoResetEvent(False)  
      
        Dim nTime As Integer = 10 * 1000  
      
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
            button1 = New System.Windows.Forms.Button()  
            progressBar1 = New System.Windows.Forms.ProgressBar()  
      
            button1.Location = New System.Drawing.Point(103, 62)  
            button1.Name = "button1"  
            button1.Size = New System.Drawing.Size(75, 23)  
            button1.TabIndex = 0  
            button1.Text = "button1"  
            button1.UseVisualStyleBackColor = True  
            progressBar1.Location = New System.Drawing.Point(40, 150)  
            progressBar1.Name = "progressBar1"  
            progressBar1.Size = New System.Drawing.Size(201, 30)  
            progressBar1.TabIndex = 1  
      
            ClientSize = New System.Drawing.Size(284, 261)  
            Controls.Add(progressBar1)  
            Controls.Add(button1)  
      
            CenterToScreen()  
        End Sub  
      
        Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click  
            progressBar1.Value = 0  
            If (wt IsNot Nothing) Then  
                wt.Abort()  
                wt.Join()  
            End If  
            wt = New Threading.Thread(AddressOf WorkerThread)  
            wt.Start()  
        End Sub  
      
        Private Sub WorkerThread()  
            Do While Not isCanceled.WaitOne(0)  
                Invoke(New Action(AddressOf DrawProgressBar))  
                Threading.Thread.S leep(1000)  
            Loop  
        End Sub  
      
        Private Sub DrawProgressBar()  
            If progressBar1.Value < 100 Then  
                progressBar1.Value += (100 / (nTime / 1000))  
                progressBar1.Refresh()  
            Else  
                isCanceled.Set()  
            End If  
        End Sub  
      
        Protected Overrides Sub OnFormClosing(ByVal e As FormClosingEventArgs)  
            If wt IsNot Nothing Then wt.Abort()  
            MyBase.OnFormClosing(e)  
        End Sub  
    End Class  
      
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.