1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
Imports System.Threading
Public Class Form1
Private trd As Thread
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
If trd.IsAlive Then
trd.Abort()
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
trd = New Thread(AddressOf ThreadTask)
trd.IsBackground = True
trd.Start()
End Sub
Private Delegate Sub UpdateProgressBarDelegate(ByVal progress%)
Private Sub UpdateProgressBarMethod(ByVal progress%)
If Not progress > 100 Then
Me.ProgressBar1.Value = progress
End If
End Sub
Private Sub ThreadTask()
Dim stp As Integer
Dim newval As Integer
Dim rnd As New Random()
Do
stp = ProgressBar1.Step * rnd.Next(-1, 2)
newval = ProgressBar1.Value + stp
If newval > ProgressBar1.Maximum Then
newval = ProgressBar1.Maximum
ElseIf newval < ProgressBar1.Minimum Then
newval = ProgressBar1.Minimum
End If
If Me.InvokeRequired Then
Me.Invoke(New UpdateProgressBarDelegate(AddressOf UpdateProgressBarMethod), newval)
End If
'ProgressBar1.Value = newval
Thread.Sleep(100)
Loop
End Sub
End Class |