在我的主窗体中,当我单击提交按钮时,将执行长时间运行的过程(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`
在这段代码中,当我第一次点击按钮时,所有图像都是勾选的。我想在每种方法结束时逐一将图像从十字架改为刻度。 我不知道我的代码有什么问题。如果有人发现任何错误,请纠正我。
答案 0 :(得分: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
答案 1 :(得分:0)
一些图片控件支持动画GIF,将动画gif放入图片控件。
如果控件没有按照您的需要更新,可能需要添加一个计时器并手动刷新控件,然后再加Application.DoEvents();
。