ใช้พวก Thread เข้ามาช่วยครับ ลองดูบทความนี้ครับ เป็น Windows Phone แต่ใช้ .NET เช่นเดียวกันครับ
Code (VB.NET)
Private Sub btnStartThread_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
btnStartThread.IsEnabled = False
Dim thread As System.Threading.Thread = New System.Threading.Thread(AddressOf ThreadStart)
thread.Start()
End Sub
Private Sub ThreadStart()
Dim intProgress As Integer = 0
'*** for Display ***'
Dim display As New Action(Of Integer)(AddressOf UpdateStatus)
'*** when Finish ***'
Dim finish As New Action(AddressOf UpdateFinish)
'*** Example for Execute
For i As Integer = 0 To 100
intProgress = i
System.Threading.Thread.Sleep(100)
Dispatcher.BeginInvoke(display, intProgress) 'Update Status
Next
Dispatcher.BeginInvoke(finish) ' When Finish
End Sub
'*** for Update Status
Private Sub UpdateStatus(ByVal intProgress As Integer)
'*** Update Progress ***'
Me.ProgressBar1.Value = intProgress
'*** Update Text Result ***'
Me.txtResult.Text = intProgress & " %"
End Sub
'*** for When Thread Complete
Private Sub UpdateFinish()
btnStartThread.IsEnabled = True
End Sub
Windows Phone and ProgressBar Using Thread/Multiple Thread (Silverlight)