alwaysVBNET
alwaysVBNET

Reputation: 3310

Updating progress bar within a multitask function

I am trying to update the progress bar which is on my Form1 using Form1.ProgressBar1.PerformStep() from a class function. I am using the async functions. There are 10 processes running but the progress bar should be updated based on the value of the records read (and later processed) within the While loop. It looks like I'm not able to update the GUI using the code below. I have tried begininvoke and invoke but not luck. Any ideas?

 Await Cheque.MultiProcessCheques()

 Public Shared Async Function MultiProcessCheques() As Task
            Dim tasks As New List(Of Task)()
            For i As Integer = 0 To 9
                Dim temp_i As Integer = i
                tasks.Add(Task.Run(Function() Cheque.CopyBinaryValueToFile(temp_i)))
            Next
            Await Task.WhenAll(tasks)
 End Function

 Public Shared Async Function CopyBinaryValueToFile(i As Integer) As Task
            Try
                Using connection = ConnectionController.GetConnection
                    Await connection.OpenAsync()
                    Using command = ConnectionController.GetCommand
                        command.CommandText = ("SELECT ch.RECORDID FROM TABLE WHERE VALUE = '%" & i & "'")
                        command.Connection = connection
                        command.CommandTimeout = 0
                        Using reader As Common.DbDataReader = Await command.ExecuteReaderAsync(CommandBehavior.SequentialAccess)
                            While Await reader.ReadAsync()
                                If reader.HasRows Then

                                End If
' update Progress bar here
                                Form1.ProgressBar1.PerformStep() 
                            End While
                        End Using
                    End Using
                End Using
            Catch ex As Exception
                MessageBox.Show("1" & ex.ToString)
            End Try
        End Function
    End Class

Upvotes: 1

Views: 381

Answers (1)

Idle_Mind
Idle_Mind

Reputation: 39122

Create a Shared Member to hold the Form1 reference in your Cheque Class, and modify your MultiProcessCheques() function to receive a reference:

Public Class Cheque

    Private Shared F1 As Form1

    Public Shared Async Function MultiProcessCheques(ByVal f1 As Form1) As Task
        Cheque.F1 = f1

        ' ... other code ...

    End Function

    Public Shared Async Function CopyBinaryValueToFile(i As Integer) As Task

        ' ... other code ...

        Cheque.F1.Invoke(Sub()
                             Cheque.F1.ProgressBar1.PerformStep()
                         End Sub)

        ' ... other code ...

    End Function

End Class

Then pass in "Me" when you call it:

Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    ' ... other code ...

    Await Cheque.MultiProcessCheques(Me)

    ' ... other code ...

End Sub

Upvotes: 2

Related Questions