Guide Contents
|
DigitalDan Home Page
|
Public Class Form1
Private Function SlowTask() as Boolean
Dim Finished as Boolean = False
For i As Integer = 1 to 20
Thread.Sleep(500)
' above line asks computer to wait half a second before continuing
Next
Finished = True
Return Finished
End Function
End Class
Public Class Form1
Private Function SlowTask() as Boolean
Dim Finished as Boolean = False
For i As Integer = 1 to 20
Thread.Sleep(500)
' above line asks computer to wait half a second before continuing
Next
Finished = True
Return Finished
End Function
'
Private Sub BtnStart_Click(sender As Object, e As EventArgs) Handles Button1.Click
BackgroundWorker1.RunWorkerAsync()
End Sub
'
Private Sub BgwMain_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
SlowTask()
End Sub
End Class
Public Class Form1
Private Function SlowTask() as Boolean
Dim Finished as Boolean = False
For i As Integer = 1 to 20
Thread.Sleep(500)
' above line asks computer to wait half a second before continuing
Next
Finished = True
Return Finished
End Function
'
Private Sub BtnStart_Click(sender As Object, e As EventArgs) Handles Button1.Click
BackgroundWorker1.RunWorkerAsync()
End Sub
'
Private Sub BgwMain_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
SlowTask()
End Sub
'
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
Label1.text="Backgroundworker Completed"
End Sub
End Class
Public Class Form1
Private Function SlowTask() as Boolean
Dim Finished as Boolean = False
For i As Integer = 1 to 20
BackgroundWorker1.ReportProgress(0, "Loop " & i.toString())
Thread.Sleep(500)
' above line asks computer to wait half a second before continuing
Next
Finished = True
Return Finished
End Function
'
Private Sub BtnStart_Click(sender As Object, e As EventArgs) Handles Button1.Click
BackgroundWorker1.WorkerReportsProgress = True
BackgroundWorker1.RunWorkerAsync()
End Sub
'
Private Sub BgwMain_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
SlowTask()
End Sub
'
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
Label1.text="Backgroundworker Completed"
End Sub
'
Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
LblProgress.Text = message
LblProgress.Refresh()
End Sub
End Class
Public Class Form1
Private Function SlowTask() as Boolean
Dim Finished as Boolean = False
For i As Integer = 1 to 20
BackgroundWorker1.ReportProgress(0, "Loop " & i.toString())
Thread.Sleep(500)
' above line asks computer to wait half a second before continuing
If BackgroundWorker1.CancellationPending Then Exit For
Next
Finished = True
Return Finished
End Function
'
Private Sub BtnStart_Click(sender As Object, e As EventArgs) Handles Button1.Click
BackgroundWorker1.WorkerReportsProgress = True
BackgroundWorker1.WorkerSupportsCancellation = True
BackgroundWorker1.RunWorkerAsync()
End Sub
'
Private Sub BgwMain_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
SlowTask()
End Sub
'
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
Label1.text="Backgroundworker Completed"
End Sub
'
Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
LblProgress.Text = message
LblProgress.Refresh()
End Sub
'
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
BackgroundWorker1.CancelAsync()
End Sub
End Class
Public Class Form1
Private Function SlowTask(par1 As String) as Boolean
Dim Finished as Boolean = False
For i As Integer = 1 to 20
BackgroundWorker1.ReportProgress(0, "Loop " & i.toString())
BackgroundWorker1.ReportProgress(0, par1 & " " & i.toString())
Thread.Sleep(500)
' above line asks computer to wait half a second before continuing
If BackgroundWorker1.CancellationPending Then Exit For
Next
Finished = True
Return Finished
End Function
'
Private Sub BtnStart_Click(sender As Object, e As EventArgs) Handles Button1.Click
BackgroundWorker1.WorkerReportsProgress = True
BackgroundWorker1.WorkerSupportsCancellation = True
Dim parm1 as String = "New Progress Messaage "
BackgroundWorker1.RunWorkerAsync(parm1)
End Sub
'
Private Sub BgwMain_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim par1 As String = e.Argument.ToString
SlowTask(par1)
End Sub
'
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
Label1.text="Backgroundworker Completed"
End Sub
'
Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
LblProgress.Text = message
LblProgress.Refresh()
End Sub
'
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
BackgroundWorker1.CancelAsync()
End Sub
End Class
Public Class Form1
Private Function SlowTask(par1 As String) as Boolean
Dim Finished as Boolean = False
For i As Integer = 1 to 20
BackgroundWorker1.ReportProgress(0, par1 & " " & i.toString())
Thread.Sleep(500)
' above line asks computer to wait half a second before continuing
If BackgroundWorker1.CancellationPending Then Exit For
Next
Finished = True
Return Finished
End Function
'
Private Sub BtnStart_Click(sender As Object, e As EventArgs) Handles Button1.Click
BackgroundWorker1.WorkerReportsProgress = True
BackgroundWorker1.WorkerSupportsCancellation = True
Dim parm1 as String = "New Progress Messaage "
BackgroundWorker1.RunWorkerAsync(parm1)
End Sub
'
Private Sub BgwMain_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim par1 As String = e.Argument.ToString
SlowTask(par1)
End Sub
'
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
Label1.text="Backgroundworker Completed"
End Sub
'
Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
LblProgress.Text = message
LblProgress.Refresh()
End Sub
'
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
BackgroundWorker1.CancelAsync()
End Sub
End Class
Public Class Form1
Private Sub FrmMain_Load(sender As Object, e As EventArgs) Handles Me.Load
Button1.Enabled = True
Button2.Enabled = False
End Sub
'
Private Function SlowTask(par1 As String) as Boolean
Dim Finished as Boolean = False
For i As Integer = 1 to 20
BackgroundWorker1.ReportProgress(0, par1 & " " & i.toString())
Thread.Sleep(500)
' above line asks computer to wait half a second before continuing
If BackgroundWorker1.CancellationPending Then Exit For
Next
Finished = True
Return Finished
End Function
'
Private Sub BtnStart_Click(sender As Object, e As EventArgs) Handles Button1.Click
Button1.Enabled = False
BackgroundWorker1.WorkerReportsProgress = True
BackgroundWorker1.WorkerSupportsCancellation = True
Dim parm1 as String = "New Progress Messaage "
Button2.Enabled = True
BackgroundWorker1.RunWorkerAsync(parm1)
End Sub
'
Private Sub BgwMain_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim par1 As String = e.Argument.ToString
SlowTask(par1)
End Sub
'
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
Button2.Enabled = False
Label1.text="Backgroundworker Completed"
Button1.Enabled = True
End Sub
'
Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
LblProgress.Text = message
LblProgress.Refresh()
End Sub
'
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
BackgroundWorker1.CancelAsync()
End Sub
End Class
Friend Shared message1 As String
Friend Shared field As New Object ' used by SyncLock to stop multiple processes accessing message1 simlutaneously
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' init Bgw
BackgroundWorker1.WorkerReportsProgress = True
BackgroundWorker1.WorkerSupportsCancellation = False
' InitTimer
Timer1.Interval = 1300
' 1300 ms = trigger about every 1.3 seconds
Timer1.Start()
' Initialise the messafe for ReportProgress
SyncLock field
message1 = "Starting Background Worker"
End SyncLock
' Start BackgroundWorker
BackgroundWorker1.RunWorkerAsync()
End Sub
'
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
BackgroundWorker1.ReportProgress(0, "") ' ReportPrograss now reports the string in message1
Timer1.Start()
' replace everything between For and Next with your background worker code
' Set message1 to string to be reported by modified BGW progress changed event.
' You must use Synclock/End Syslock whenever message1 accessed but keep code inside SysLock/End SysLock to absolute minimum
' BGW report progress is now controlled by the timer tick event and picks up message1 regardless of ReportProgress parameters
For i As Integer = 0 To 20
System.Threading.Thread.Sleep(500)
' thread.sleep used to mimic a scenario where code takes a long time to process
SyncLock field
message1 = "Loop = " & i.ToString
' You must update message1 frequently (e.g. each pass of a loop) - user may expect value to change after most timer ticks
End SyncLock
Next
SyncLock field
message1 = "Finished Background Worker"
End SyncLock
BackgroundWorker1.ReportProgress(100, "")
' note - ReportPrograss reports contents of the string message1
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
Timer1.Stop()
End Sub
Private Sub BgwTimer_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim temp As String = ""
SyncLock field
temp = message1
End SyncLock
BackgroundWorker1.ReportProgress(2, temp)
End Sub
Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
LblProgress.Text = message1
LblProgress.Refresh()
End Sub