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`

このコードでは、ボタンをクリックすると、最初はすべての画像がチェックされます。各メソッドの最後で、イメージをクロスからティックに 1 つずつ変更したいと考えています。私のコードの何が問題なのかわかりません..誰かが間違いを見つけたら、私を修正してください.

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

一部の画像コントロールはアニメーション GIF をサポートしており、アニメーション GIF を画像コントロールに入れます。

コントロールが希望どおりに更新されない場合は、タイマーを追加して、Application.DoEvents();後でコントロールを手動で更新する必要があるかもしれません。

于 2013-10-29T08:05:25.397 に答える