16,721
社区成员




If Number = 0 Then
frmProgress.Show()
frmProgress.prgbar.Value = Number
frmProgress.Label1.Text = str_caption
ElseIf Number = 100 Then
frmProgress.prgbar.Value = Number
frmProgress.Label1.Text = str_caption
frmProgress.Close()
frmProgress.Dispose()
Else
frmProgress.prgbar.Value = Number
frmProgress.Label1.Text = str_caption
End If
frmProgress.Refresh()
'这样可以进度条窗体可以正常显示 并不需要加 Application.DoEvents()
Public Class Form1
Private m_Count As Integer
Dim PrcentForm As Form2
Private Delegate Sub PrcentChangeDelegate(ByVal Prcent As Integer)
Public ReadOnly Property Count() As Integer
Get
Return m_Count
End Get
End Property
Private Sub OKbtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles OKbtn.Click
LV.Items.Clear()
PrcentForm = Form2
PrcentForm.Show()
Dim i As Integer = 0
While i <= m_Count '模拟加载数据
PrcentForm.Invoke(New PrcentChangeDelegate(AddressOf PrcentForm.DisPlayVal), New Object() {CInt((i / (m_Count)) * 100)})
LV.Items.Add(i)
LV.Items(i).SubItems.Add(i)
i += 1
End While
PrcentForm.Close()
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
LV.Columns.Add("数据", 200, HorizontalAlignment.Center)
m_Count = 10000
Control.CheckForIllegalCrossThreadCalls = False
End Sub
End Class
Public Sub DisPlayVal(ByVal Prcent As Integer)
PB.Value = Prcent
End Sub
Public Class Form3
Private Delegate Function DisPlayText(ByVal Intput As Long)
Private Delegate Sub DisValue(ByVal Value As Integer)
Private exv As Boolean
Private Sub PbValue(ByVal Value As Integer)
PB.Value = Value
End Sub
Private Sub metext(ByVal text As Long)
Me.Text = text.ToString
End Sub
Dim iresult As IAsyncResult
Dim A As DisPlayText
Private Function Cal(ByVal Intput As Long) As Integer
Dim i As Long
Dim t As Long
For i = 0 To Intput
t = i + 1
Me.Invoke(New DisValue(AddressOf PbValue), CInt((i / Intput) * 100))
Next
Return t
End Function
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
If Not exv Then
Try
A = New DisPlayText(AddressOf Cal)
iresult = A.BeginInvoke(100000, New AsyncCallback(AddressOf CallBack), Nothing)
Catch ex As Exception
Finally
exv = True
End Try
End If
End Sub
Private Sub CallBack()
Try
Dim i As Integer = A.EndInvoke(iresult)
Me.Invoke(New DisValue(AddressOf metext), i)
Catch ex As Exception
Finally
exv = False
End Try
End Sub
End Class
Imports System.ComponentModel
Public Class Form1
Private m_Count As Integer '
Dim PrcentForm As Form2
Private Delegate Sub PrecentChangeDelegate(ByVal Precent As Integer)
Private Delegate Sub RunDelegate()
Private Delegate Sub CompleteDelegate()
Private Asyresult As IAsyncResult
Private List As BindingList(Of Datalist)
Private Run As RunDelegate
Public Sub DisPlayPrecent(ByVal Precent As Integer)
PrcentForm.PB.Value = Precent
If Precent = 100 Then
PrcentForm.Close()
End If
End Sub
Private Sub OKbtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles OKbtn.Click
m_Count = 40000
PrcentForm = Form2
PrcentForm.Show()
Run = New RunDelegate(AddressOf LoadData) '异步调用数据加载过程
Asyresult = Run.BeginInvoke(New AsyncCallback(AddressOf CallBack), Nothing)
End Sub
Private Sub CallBack() '通过回调实现绑定
Run.EndInvoke(Asyresult)
Me.Invoke(New CompleteDelegate(AddressOf BindingData))
End Sub
Private Sub BindingData() '绑定到DataGridView1
Me.DataGridView1.DataSource = List
End Sub
Private Sub LoadData() '模拟加载数据
List = New BindingList(Of Datalist)
List.Clear()
Dim i As Integer = 0
While i <= m_Count
Dim T As New Datalist(i)
Me.Invoke(New PrecentChangeDelegate(AddressOf DisPlayPrecent), New Object() {CInt(i / m_Count * 100)})
List.Add(T)
i += 1
End While
End Sub
End Class
Public Class Datalist '数据模型
Private m_Number As Integer
Public ReadOnly Property Number()
Get
Return m_Number
End Get
End Property
Public Sub New(ByVal I As Integer)
m_Number = I
End Sub
End Class