更新多任务功能中的进度条

时间:2015-08-10 16:03:52

标签: vb.net

我正在尝试使用类函数中的Form1.ProgressBar1.PerformStep()更新我的Form1上的进度条。我正在使用异步功能。有10个进程正在运行,但进度条应根据While loop内读取(以后再处理)的记录值进行更新。看起来我无法使用下面的代码更新GUI。我尝试了begininvokeinvoke,但没有运气。有什么想法吗?

 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

1 个答案:

答案 0 :(得分:2)

创建一个共享成员以在Check Class中保存Form1引用,并修改MultiProcessCheques()函数以接收引用:

$rootScope.$new

然后传入"我"当你打电话时:

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