Avatar of PeterBaileyUk
PeterBaileyUk

asked on 

Vb.net progressbar with timer1 in visual studio win app

I would like to make my progressbar move forward every second, when it reaches the bar width then goes back to the beginning and keeps doing that until the timer is stopped. its more of a working indication as I am starting and stopping in on a sql usp call that takes about 4 minutes. currently the bar does not show any progress

    Private Sub Panel1_Click(sender As Object, e As EventArgs) Handles Panel1.Click
        MessageBox.Show("Short Strings will be created, click to continue, this process. its 1 sql event which takes about 4 minutes, so wait for the next message as progress bar wont update with this procedure call. after clicking ok")
        Me.Timer1.Enabled = True
        Me.Timer1.Interval = 1 * 1000
        Timer1.Start()
        Call CreateAllStrs()
        Timer1.Stop()
        MessageBox.Show("Short Strings Created")
        Me.Timer1.Enabled = False
    End Sub

Open in new window



    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick


        If ProgressBar2.Value <= 10000 Then
            ProgressBar2.Value = ProgressBar2.Value + 1
            ProgressBar2.PerformStep()
        Else
            ProgressBar2.Value = 1

        End If
    End Sub

Open in new window

Visual Basic.NETMicrosoft Visual Studio

Avatar of undefined
Last Comment
PeterBaileyUk
ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of PeterBaileyUk
PeterBaileyUk

ASKER

How do i report the progress done when its not computable ie one sql statement running? doesnt this method require the same? Ive read the microsoft help on this but not seeing how i apply it in my code.
SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of PeterBaileyUk
PeterBaileyUk

ASKER

I did Mike's solution and its almost there but not quite, when the form opens the progress indicator now moves in the marquee fashion as described but when I click the sql process to run with a button the progress bar stops moving and when the sql process is done the progress indicator then starts moving again. in essence its the reverse now of how i imagined.
Avatar of ktaczala
ktaczala
Flag of United States of America image

The SQL process needs to run as a background worker job, the progressbar should be foreground.
When sql complete,s background complete event  can close the progressbar.
See the first answer in your question by Ryan Chong to move that SQL process to a background thread with the BackgroundWorker() control.
Avatar of PeterBaileyUk
PeterBaileyUk

ASKER

aha ok its a combination of the two ok will give that a go
Avatar of PeterBaileyUk
PeterBaileyUk

ASKER

Ok ive created the demo here:
https://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx

and from that ive got to the code in my form1 class with no errors and attached the async to the panel that has the onclick event that makes the call to the usp. the bit I dont know despite looking at the fibinachi example on the microsoft site is:do i need do anything more.

the code is here (its not all of it only the relevant bits so far the sub that does the work is CreateAllStrs():

Imports System.ComponentModel

Public Class Form1

    Dim AllListBox As New List(Of ListBox)()
    Private dtWords As DataTable

    'Private backgroundWorker1 As BackgroundWorker = New BackgroundWorker
    Public Sub New()
        InitializeComponent()
        BackgroundWorker1.WorkerReportsProgress = True
        BackgroundWorker1.WorkerSupportsCancellation = True
    End Sub
    Private Sub startAsyncButton_Click(ByVal sender As System.Object,
    ByVal e As System.EventArgs) Handles Panel1.Click
        If backgroundWorker1.IsBusy <> True Then
            ' Start the asynchronous operation.
            backgroundWorker1.RunWorkerAsync()
        End If
    End Sub

    Private Sub cancelAsyncButton_Click(ByVal sender As System.Object,
    ByVal e As System.EventArgs) Handles StrShortCancel.Click
        If backgroundWorker1.WorkerSupportsCancellation = True Then
            ' Cancel the asynchronous operation.
            backgroundWorker1.CancelAsync()
        End If
    End Sub

    'This Event handler() Is where the time-consuming work Is done.
    Private Sub backgroundWorker1_DoWork(ByVal sender As System.Object,
    ByVal e As DoWorkEventArgs) Handles backgroundWorker1.DoWork
        Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
        Dim i As Integer

        For i = 1 To 10
            If (worker.CancellationPending = True) Then
                e.Cancel = True
                Exit For
            Else
                ' Perform a time consuming operation and report progress.
                System.Threading.Thread.Sleep(500)
                worker.ReportProgress(i * 10)
            End If
        Next
    End Sub

    'This Event handler() updates the progress.
    Private Sub backgroundWorker1_ProgressChanged(ByVal sender As System.Object,
    ByVal e As ProgressChangedEventArgs) Handles backgroundWorker1.ProgressChanged
        ResultLabel.Text = (e.ProgressPercentage.ToString() + "%")
        Me.ProgressBar2.Value = e.ProgressPercentage
    End Sub

    ' This event handler deals with the results of the background operation.
    Private Sub backgroundWorker1_RunWorkerCompleted(ByVal sender As System.Object,
    ByVal e As RunWorkerCompletedEventArgs) Handles backgroundWorker1.RunWorkerCompleted
        If e.Cancelled = True Then
            ResultLabel.Text = "Canceled!"
        ElseIf e.Error IsNot Nothing Then
            ResultLabel.Text = "Error: " & e.Error.Message
        Else
            ResultLabel.Text = "Done!"
        End If
    End Sub

    Public Sub CreateAllStrs()


        'ProgressBar2.Visible = True
        'ProgressBar2.Minimum = 0
        '    ProgressBar2.Maximum = 10000
        '    ProgressBar2.Value = 0
        '    ProgressBar2.Step = 1
        'LBLBulkWait.Visible = True
        Dim StrProcName As String
        StrProcName = "usp_CreateStrStringsPlusTidyup"

        Dim connectionString As String = ConfigurationManager.ConnectionStrings("Dictionary").ConnectionString

        Using conn As New SqlConnection(connectionString)
            Using cmd As New SqlCommand(StrProcName, conn)
                cmd.CommandTimeout = 0
                cmd.CommandType = CommandType.StoredProcedure
                conn.Open()


                cmd.ExecuteScalar()
            End Using
        End Using
        'ProgressBar2.Visible = False
        'LBLBulkWait.Visible = False
    End Sub

Open in new window

Avatar of PeterBaileyUk
PeterBaileyUk

ASKER

it runs but the createallstrs doesnt seem bound into the background worker so far.
Avatar of PeterBaileyUk
PeterBaileyUk

ASKER

I think ive cracked it i put the call to createstrs in the dowork now the progress bar progresses.. why is there a .sleep?

    Private Sub backgroundWorker1_DoWork(ByVal sender As System.Object,
    ByVal e As DoWorkEventArgs) Handles backgroundWorker1.DoWork
        Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
        Dim i As Integer

        For i = 1 To 10
            If (worker.CancellationPending = True) Then
                e.Cancel = True
                Exit For
            Else
                ' Perform a time consuming operation and report progress.

                Call CreateAllStrs()

                System.Threading.Thread.Sleep(500)
                worker.ReportProgress(i * 10)
            End If
        Next
    End Sub

Open in new window

Avatar of PeterBaileyUk
PeterBaileyUk

ASKER

thank you
Visual Basic.NET
Visual Basic.NET

Visual Basic .NET (VB.NET) is an object-oriented programming language implemented on the .NET framework, but also supported on other platforms such as Mono and Silverlight. Microsoft launched VB.NET as the successor to the Visual Basic language. Though it is similar in syntax to Visual Basic pre-2002, it is not the same technology,

96K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo