进度条运行时进行处理

时间:2015-05-13 09:42:53

标签: vb.net

我知道知识很昂贵,但有谁想帮助我 我想在进度条运行时运行进程,
我尝试使用此代码

Public Class Form2
Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Call Prcss()  
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    If Progress.Value < 100 Then
        Progress.Value += 2
    ElseIf Progress.Value = 100 Then
        Timer1.Stop()
        Form1.Show()
        Me.Hide()
    End If
End Sub
Private Sub Prcss()
    With Progress
        .Value = 0
        Threading.Thread.Sleep(450)
        Label1.Text = "Renewing Custom Content"
        .Value = 20
        Threading.Thread.Sleep(450)
        Label1.Text = "Getting Information"
        .Value = 50
        Threading.Thread.Sleep(450)
        Label1.Text = "Downloading Udpdate"
        .Value = 70
        Threading.Thread.Sleep(450)
        Label1.Text = "Ready to Start"
        .Value = 100
    End With
End Sub

我不知道我的错误在哪里,我在书上看到了这一点。

1 个答案:

答案 0 :(得分:0)

尝试使用BackgroundWorkerThread

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    BackgroundWorker1.RunWorkerAsync()
End Sub

Private Sub BackgroundWorker1_DoWork(sender As Object, e As ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    For i As Integer = 1 To 1000
        BackgroundWorker1.ReportProgress(CInt(i / 10))
        Threading.Thread.Sleep(500)
    Next
End Sub

Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
    ProgressBar1.Value = e.ProgressPercentage
End Sub

Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
    MessageBox.Show("Finished!")
End Sub

这是基本示例,需要您将BackgroundWorker添加到表单中。您可以在此处查看帮助:https://msdn.microsoft.com/es-es/library/cc221403(v=vs.95).aspx