Hi VBForums,
I am invoking delegates in a backgroundworker so they don't block the UI. I pass in a delegate using AddressOf to a method called StartJob, which invokes the delegate in the backgroundworker. This works fine, but if the method that gets invoked in the DoWork event is a long running, UI Blocking operation (which it is) I can't seem to figure out how to cancel it if need be. Here are the example classes and form required to understand what I'm talking about!
Example class 1:
Imports System.ComponentModel
Public Class Job
Public WithEvents _bw As BackgroundWorker
Public Delegate Sub StartDel()
Public _del As StartDel
Public Event JobComplete()
Public Event ProgressUpdated()
Public Property Name
Public Property Type
Public Property Progress
Public Property Cancelled
Public Sub StartJob(ByVal del As StartDel)
_del = del
_bw = New BackgroundWorker
_bw.WorkerSupportsCancellation = True
_bw.WorkerReportsProgress = True
_bw.RunWorkerAsync()
End Sub
Public Sub StopJob()
Me.Cancelled = True
End Sub
Private Sub bw_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles _bw.DoWork
If Me.Cancelled = True Then
e.Cancel
Else
_del.Invoke()
End If
End Sub
End Class
Example Class 2
Public Class Test
Sub Start()
Work()
End Sub
Sub Work()
For i = 0 To 10
System.Threading.Thread.Sleep(2000)
MsgBox("Work done")
Next
End Sub
End Class
Example Form 1
Public Class TestForm
Private t As New Test
Private j As New Job
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
j.StartJob(AddressOf t.Start)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
j.StopJob()
End Sub
End Class
Calling j.StopJob doesn't stop the loop that gets invoked in the BG Worker...why? :ehh:
EDIT: Realized I had a property in the Job class called Job...was unnecessary so I removed it, woops. Still need an answer though!
I am invoking delegates in a backgroundworker so they don't block the UI. I pass in a delegate using AddressOf to a method called StartJob, which invokes the delegate in the backgroundworker. This works fine, but if the method that gets invoked in the DoWork event is a long running, UI Blocking operation (which it is) I can't seem to figure out how to cancel it if need be. Here are the example classes and form required to understand what I'm talking about!
Example class 1:
Imports System.ComponentModel
Public Class Job
Public WithEvents _bw As BackgroundWorker
Public Delegate Sub StartDel()
Public _del As StartDel
Public Event JobComplete()
Public Event ProgressUpdated()
Public Property Name
Public Property Type
Public Property Progress
Public Property Cancelled
Public Sub StartJob(ByVal del As StartDel)
_del = del
_bw = New BackgroundWorker
_bw.WorkerSupportsCancellation = True
_bw.WorkerReportsProgress = True
_bw.RunWorkerAsync()
End Sub
Public Sub StopJob()
Me.Cancelled = True
End Sub
Private Sub bw_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles _bw.DoWork
If Me.Cancelled = True Then
e.Cancel
Else
_del.Invoke()
End If
End Sub
End Class
Example Class 2
Public Class Test
Sub Start()
Work()
End Sub
Sub Work()
For i = 0 To 10
System.Threading.Thread.Sleep(2000)
MsgBox("Work done")
Next
End Sub
End Class
Example Form 1
Public Class TestForm
Private t As New Test
Private j As New Job
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
j.StartJob(AddressOf t.Start)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
j.StopJob()
End Sub
End Class
Calling j.StopJob doesn't stop the loop that gets invoked in the BG Worker...why? :ehh:
EDIT: Realized I had a property in the Job class called Job...was unnecessary so I removed it, woops. Still need an answer though!