0

在我的主窗体中,当我单击提交按钮时,将执行长时间运行的进程(5 个方法)。那时,我只想用 5 个交叉图像显示个人形式。在方法一一完成时,我想将交叉图像更改为刻度图像。我尝试了如下代码:

 Private Sub btnRetrieve_Click(sender As System.Object, e As System.EventArgs) Handles btnRetrieve.Click
 > 
    Try
        If Not BackgroundWorker1.IsBusy Then
            BackgroundWorker1.RunWorkerAsync()
        End If
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    Try
        Retrieve()         
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

Public Sub Retrieve()
    Try
        RetrieveForm.Show()
        Dim thrd1 As New Thread(New ThreadStart(AddressOf RetrieveClient))
        Dim thrd2 As New Thread(New ThreadStart(AddressOf RetrieveProject))
        Dim thrd3 As New Thread(New ThreadStart(AddressOf RetrieveModule))
        Dim thrd4 As New Thread(New ThreadStart(AddressOf RetrievePerson))
        Dim thrd5 As New Thread(New ThreadStart(AddressOf RetrieveStatus))
        Dim thrd6 As New Thread(New ThreadStart(AddressOf RetrievePriority))
        thrd1.Start()
        tickimage(RetrieveForm.pbClient)
        thrd2.Start()
        tickimage(RetrieveForm.pbProject)
        thrd3.Start()
        tickimage(RetrieveForm.pbModule)
        thrd4.Start()
        tickimage(RetrieveForm.pbUsers)
        thrd5.Start()
        tickimage(RetrieveForm.pbStatus)
        thrd6.Start()
        tickimage(RetrieveForm.pbPriority)
        Application.DoEvents()
        MessageBox.Show("Retrieved")
        RetrieveForm.Close()
        InitialLoad()
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

Public Sub tickimage(ByVal pbId As PictureBox)
    Try
        pbId.InitialImage = Nothing
        Dim img As Image = Resources.tick1
        pbId.Image = img
        pbId.SizeMode = PictureBoxSizeMode.StretchImage
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub`

在这段代码中,当我第一次单击按钮时,所有图像都会打勾。我想在每种方法的末尾将图像从交叉更改为逐个打勾。我不知道我的代码有什么问题。如果有人发现其中有任何错误,请纠正我。

4

2 回答 2

0

试试这个...

Private R As New Random

Private Sub btnRetrieve_Click(sender As System.Object, e As System.EventArgs) Handles btnRetrieve.Click
    Try
        If Not BackgroundWorker1.IsBusy Then
            RetrieveForm.Show()
            BackgroundWorker1.RunWorkerAsync()
        End If
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    Try
        Retrieve()
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

Public Sub Retrieve()
    Try
        Dim thrd1 As New Thread(AddressOf RetrieveClient)
        Dim thrd2 As New Thread(AddressOf RetrieveProject)
        Dim thrd3 As New Thread(AddressOf RetrieveModule)
        Dim thrd4 As New Thread(AddressOf RetrievePerson)
        Dim thrd5 As New Thread(AddressOf RetrieveStatus)
        Dim thrd6 As New Thread(AddressOf RetrievePriority)

        thrd1.Start()
        thrd2.Start()
        thrd3.Start()
        thrd4.Start()
        thrd5.Start()
        thrd6.Start()

        thrd1.Join()
        thrd2.Join()
        thrd3.Join()
        thrd4.Join()
        thrd5.Join()
        thrd6.Join()
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

Private Sub RetrieveClient()
    Thread.Sleep(R.Next(3000, 10001)) ' 3 to 10 seconds of "work"

    ' ... code ...

    BackgroundWorker1.ReportProgress(1)
End Sub

Private Sub RetrieveProject()
    Thread.Sleep(R.Next(3000, 10001)) ' 3 to 10 seconds of "work"

    ' ... code ...

    BackgroundWorker1.ReportProgress(2)
End Sub

Private Sub RetrieveModule()
    Thread.Sleep(R.Next(3000, 10001)) ' 3 to 10 seconds of "work"

    ' ... code ...

    BackgroundWorker1.ReportProgress(3)
End Sub

Private Sub RetrievePerson()
    Thread.Sleep(R.Next(3000, 10001)) ' 3 to 10 seconds of "work"

    ' ... code ...

    BackgroundWorker1.ReportProgress(4)
End Sub

Private Sub RetrieveStatus()
    Thread.Sleep(R.Next(3000, 10001)) ' 3 to 10 seconds of "work"

    ' ... code ...

    BackgroundWorker1.ReportProgress(5)
End Sub

Private Sub RetrievePriority()
    Thread.Sleep(R.Next(3000, 10001)) ' 3 to 10 seconds of "work"

    ' ... code ...

    BackgroundWorker1.ReportProgress(6)
End Sub

Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
    Select Case e.ProgressPercentage
        Case 1
            tickimage(RetrieveForm.pbClient)
        Case 2
            tickimage(RetrieveForm.pbProject)
        Case 3
            tickimage(RetrieveForm.pbModule)
        Case 4
            tickimage(RetrieveForm.pbUsers)
        Case 5
            tickimage(RetrieveForm.pbStatus)
        Case 6
            tickimage(RetrieveForm.pbPriority)
    End Select
End Sub

Public Sub tickimage(ByVal pbId As PictureBox)
    Try
        pbId.InitialImage = Nothing
        Dim img As Image = My.Resources.tick1
        pbId.Image = img
        pbId.SizeMode = PictureBoxSizeMode.StretchImage
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
    MessageBox.Show("Retrieved")
    RetrieveForm.Close()
    InitialLoad()
End Sub

Private Sub InitialLoad()
    Debug.Print("InitialLoad()")
End Sub
于 2013-10-29T08:00:08.930 回答
0

有的图片控件支持动图,把动图放到图片控件中。

如果控件没有按照您的意愿更新,也许您需要添加一个计时器并手动刷新控件,Application.DoEvents();然后再进行一次。

于 2013-10-29T08:05:25.397 回答