Belajar Tutorial Vb.Net Pemula : Penggunaan Class Background Worker dan ProgressBar Vb.Net, membuat aplikasi Background Worker dan ProgressBar Vb.Net khusus pemula, tutorial vb.net selengkapnya di sector code
Class Background Worker - Cara penggunaan Class Background Worker dalam aplikasi vb.net memungkinkan developer vb.net untuk melihat prosess data yang sedang berlangsung, serta pengguna aplikasi akan bisa menggunakan aplikasi yang sama dengan melakukan perintah lain dalam waktu yang sama, sehingga penggunaan Background Worker dan ProgressBar Vb.Net akan sangat user friendly dalam aplikasi windows form. simak terus bagaimana cara pembuatan aplikasinya ya..
Silahkan share dan comment jika bermanfaat :)
Aplikasi Sederhana menggunakan Background Worker dan ProgressBar
Buatlah Project baru dengan nama "BackgroundWorker", dan beri beberapa komponen berikut :- ProgressBar
- button (2 buah)
- label
- groupbox
- bacground worker
Deklarasikan waktu untuk Looping
letakkan code berikut dalam class form1.vbDim _Loop As Integer = 0
Code untuk Tombol Start
Private Sub TombolStart_Click(sender _ As System.Object, e As System.EventArgs) _ Handles TombolStart.Click _Loop = 30 Me.TombolStart.Enabled = False Me.TombolStop.Enabled = True BgWorker.RunWorkerAsync() End Sub
Code Tombol Batal
Private Sub TombolStop_Click(sender _ As System.Object, e As System.EventArgs) _ Handles TombolStop.Click If BgWorker.IsBusy Then If BgWorker.WorkerSupportsCancellation Then BgWorker.CancelAsync() End If End If Me.TombolStart.Enabled = True Me.TombolStop.Enabled = False End Sub
Bakground Worker_DoWork()
Private Sub BgWorker_DoWork(ByVal _ sender As Object, _ ByVal e As _ System.ComponentModel.DoWorkEventArgs) _ Handles BgWorker.DoWork For x As Integer = 0 To _Loop If BgWorker.CancellationPending Then e.Cancel = True Exit For End If System.Threading.Thread.Sleep(1000) BgWorker.ReportProgress(CInt((x / _Loop) * 100)) UpdateLabel(Me.Status, _ FormatPercent(x / _Loop, 2)) Next End Sub
Update ProgressBarr Value
Private Sub BgWorker_ProgressChanged(ByVal _ sender As Object, ByVal e As _ System.ComponentModel.ProgressChangedEventArgs) _ Handles BgWorker.ProgressChanged Me.ProgressBar1.Value = _ e.ProgressPercentage End Sub
Bakcgorund Worker Complated
Private Sub BgWorker_RunWorkerCompleted(ByVal _ sender As Object, ByVal e As _ System.ComponentModel.RunWorkerCompletedEventArgs) _ Handles BgWorker.RunWorkerCompleted If e.Cancelled Then Me.Status.Text = "Dibatalkan!" Else Me.Status.Text = "Komplit!" End If End Sub
Set Label Delegate
Delegate Sub SetLabelText_Delegate(ByVal _ [Label] As Label, ByVal [text] As String)
Update Label
Private Sub UpdateLabel(ByVal _ [Label] As Label, ByVal [text] As String) If [Label].InvokeRequired Then Dim MyDelegate As New _ SetLabelText_Delegate(AddressOf UpdateLabel) Me.Invoke(MyDelegate, New Object() _ {[Label], [text]}) Else [Label].Text = [text] End If End SubSimpan aplikasi Background Worker dan ProgressBar Vb.Net kamu dan coba jalankan, berikut kami sertakan source code secara lengkapnya :
Public Class Form1 Dim _Loop As Integer = 0 Private Sub TombolStart_Click(sender _ As System.Object, e As System.EventArgs) _ Handles TombolStart.Click _Loop = 30 Me.TombolStart.Enabled = False Me.TombolStop.Enabled = True BgWorker.RunWorkerAsync() End Sub Private Sub TombolStop_Click(sender _ As System.Object, e As System.EventArgs) _ Handles TombolStop.Click If BgWorker.IsBusy Then If BgWorker.WorkerSupportsCancellation Then BgWorker.CancelAsync() End If End If Me.TombolStart.Enabled = True Me.TombolStop.Enabled = False End Sub Private Sub BgWorker_DoWork(ByVal _ sender As Object, _ ByVal e As _ System.ComponentModel.DoWorkEventArgs) _ Handles BgWorker.DoWork For x As Integer = 0 To _Loop If BgWorker.CancellationPending Then e.Cancel = True Exit For End If System.Threading.Thread.Sleep(1000) BgWorker.ReportProgress(CInt((x / _Loop) * 100)) UpdateLabel(Me.Status, _ FormatPercent(x / _Loop, 2)) Next End Sub Private Sub BgWorker_ProgressChanged(ByVal _ sender As Object, ByVal e As _ System.ComponentModel.ProgressChangedEventArgs) _ Handles BgWorker.ProgressChanged Me.ProgressBar1.Value = _ e.ProgressPercentage End Sub Private Sub BgWorker_RunWorkerCompleted(ByVal _ sender As Object, ByVal e As _ System.ComponentModel.RunWorkerCompletedEventArgs) _ Handles BgWorker.RunWorkerCompleted If e.Cancelled Then Me.Status.Text = "Dibatalkan!" Else Me.Status.Text = "Komplit!" End If End Sub Delegate Sub SetLabelText_Delegate(ByVal _ [Label] As Label, ByVal [text] As String) Private Sub UpdateLabel(ByVal _ [Label] As Label, ByVal [text] As String) If [Label].InvokeRequired Then Dim MyDelegate As New _ SetLabelText_Delegate(AddressOf UpdateLabel) Me.Invoke(MyDelegate, New Object() _ {[Label], [text]}) Else [Label].Text = [text] End If End Sub End Class
Video Tutorial Background Worker dan ProgressBar Vb.Net
Silahkan share dan comment jika bermanfaat :)
COMMENTS