Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports System.Threading
Delegate Sub SetValueCallback(ByVal value As Integer)
Public Class Form1
Private Sub Foo()
Dim i As Integer = 1
While i <= 100
Thread.Sleep(100)
SetProcessBarValue(i)
SetLabelValue(i)
System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
End While
End Sub
Private Sub SetLabelValue(ByVal value As Integer)
' InvokeRequired required compares the thread ID of the
' calling thread to the thread ID of the creating thread.
' If these threads are different, it returns true.
If Me.Label1.InvokeRequired Then
Dim d As New SetValueCallback(AddressOf SetLabelValue)
Me.Invoke(d, New Object() {value})
Else
Me.Label1.Text = value.ToString() + "%"c
End If
End Sub
Private Sub SetProcessBarValue(ByVal value As Integer)
' InvokeRequired required compares the thread ID of the
' calling thread to the thread ID of the creating thread.
' If these threads are different, it returns true.
If Me.ProgressBar1.InvokeRequired Then
Dim d As New SetValueCallback(AddressOf SetProcessBarValue)
Me.Invoke(d, New Object() {value})
Else
Me.ProgressBar1.Value = value
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim t As New Thread(New ThreadStart(AddressOf Foo))
t.Start()
End Sub
End Class
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports System.Threading
Delegate Sub SetValueCallback(ByVal value As Integer)
Public Class Form1
Private Sub Foo()
Dim i As Integer = 1
While i <= 100
Thread.Sleep(100)
SetProcessBarValue(i)
SetLabelValue(i)
System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
End While
End Sub
Private Sub SetLabelValue(ByVal value As Integer)
' InvokeRequired required compares the thread ID of the
' calling thread to the thread ID of the creating thread.
' If these threads are different, it returns true.
If Me.Label1.InvokeRequired Then
Dim d As New SetValueCallback(AddressOf SetLabelValue)
Me.Invoke(d, New Object() {value})
Else
Me.Label1.Text = value.ToString() + "%"c
End If
End Sub
Private Sub SetProcessBarValue(ByVal value As Integer)
' InvokeRequired required compares the thread ID of the
' calling thread to the thread ID of the creating thread.
' If these threads are different, it returns true.
If Me.ProgressBar1.InvokeRequired Then
Dim d As New SetValueCallback(AddressOf SetProcessBarValue)
Me.Invoke(d, New Object() {value})
Else
Me.ProgressBar1.Value = value
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim t As New Thread(New ThreadStart(AddressOf Foo))
t.Start()
End Sub
End Class
【VB.net 线程 ProgressBar.rar】点击下载此文件
|
暂时没有评论
发表评论 - 不要忘了输入验证码哦! |