簡體   English   中英

加載長時間運行的進程時更改圖片框中的圖像-vb.net

[英]Change images in pictureboxes while loading long running process -vb.net

在我的主窗體中,當我單擊“提交”按鈕時,將執行長時間運行的過程(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`

在這段代碼中,當我單擊按鈕時,它本身是第一次顯示所有圖像。 我想將圖像從交叉更改為在每種方法結束時逐個刻度。 我不知道我的代碼有什么問題。如果有人發現任何錯誤,請更正我。

試試這個...

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

一些圖片控件支持動畫gif,請將動畫gif放入圖片控件。

如果控件沒有按照您希望的方式更新,則可能需要添加一個計時器,並使用Application.DoEvents();手動刷新控件Application.DoEvents(); 然后。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM