Stopping Background Worker

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I need to stop the application if certain things happen, such as no data comes back. I have the following:
<pre class="prettyprint" style=" Imports System.ComponentModel

Public Class frmConsolidation
Dim workRequest As Integer
Dim noOfPallets As Integer
Dim WithEvents worker As New BackgroundWorker
Dim bolProcessing As New BOLprocessing

Public Sub New()
InitializeComponent()
worker.WorkerReportsProgress = True
worker.WorkerSupportsCancellation = True
AddHandler worker.DoWork, AddressOf worker_DoWork
AddHandler worker.ProgressChanged, AddressOf worker_ProgressChanged
AddHandler worker.RunWorkerCompleted, AddressOf worker_RunWorkerCompleted
End Sub

Private Sub btnProcess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnProcess.Click
btnProcess.Enabled = False
btnQuit.Enabled = False
worker.WorkerSupportsCancellation = True
worker.RunWorkerAsync()
End Sub

Private Sub worker_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles worker.DoWork
Dim shipmentID As Integer = 0
Dim returnID As Integer = 0
workRequest = mtxtbxWorkRequestNo.Text Get the text of the Work Request textbox and store it in workRequest
noOfPallets = mtxtbxNoOfPallets.Text Get the text of the No of Pallets textbox and store it in noOfPallets
workRequest = Trim(workRequest)
noOfPallets = Trim(noOfPallets)
This is an attempt to find out if the thread is in a cancellation pending state
If (worker.CancellationPending) Then
e.Cancel = True
End If

If (rbtnOne.Checked = True) Then
worker.ReportProgress(0, "Starting....")
returnID = bolProcessing.updateInContainersOne(workRequest, noOfPallets, worker) This block right here should kill the thread/application<br/> I have verified that the application is returning 1 when appropriate, that this block<br/> is cathching it and the code is being executed<br/> If (returnID = 1) Then worker.ReportProgress(0, "You requested more pallets than are available.")
worker.ReportProgress(0, "BOL Consolidation cancelled.")
worker.CancelAsync()
End If
If (returnID = 0) Then
bolProcessing.updateShipmentIDOne(workRequest, worker)
End If
End If

If (rbtnTwo.Checked = True) Then
worker.ReportProgress(0, "Starting....")
returnID = bolProcessing.updateInContainersTwo(workRequest, noOfPallets, worker)
If (returnID = 1) Then
worker.ReportProgress(0, "You requested more pallets than are available.")
worker.ReportProgress(0, "BOL Consolidation cancelled.")
worker.CancelAsync()
End If
If (returnID = 0) Then
bolProcessing.updateShipmentIDTwo(workRequest, worker)
End If
End If
End Sub

Private Sub worker_ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs)
lblOutput.AppendText(e.UserState.ToString())
lblOutput.AppendText(Environment.NewLine)
lblOutput.Refresh()
progressBar.Increment(15) This is an attempt to recognized that the thread should be stopped
If (worker.CancellationPending = True) Then
worker.Dispose()
End If
End Sub

Private Sub worker_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs)
If (e.Cancelled) Then
lblOutput.Text = "Canceled!"
ElseIf e.Error IsNot Nothing Then
lblOutput.Text = "Error: " & e.Error.Message
Else
lblOutput.Text = "Done!"
btnProcess.Enabled = True
btnQuit.Enabled = True
End If
End Sub
End Class [/code]
The issue is that the application stops processing when it needs to. But it starts the whole thread over. For example, if the number of pallets the user wants to consolidate down to is greater than the total number of current pallets, the application recognizes
this and stops processing the request. But for some reason, it loops back around to the start of the thread and starts processing the request again. It just stays in this loop. I am wondering if I should be setting the thread to a complete state. I tried that
and it didnt seem to work.

<br/>

View the full article
 
Back
Top