Delegate Sub AddtoListCallback(ByVal [text] As String)
Dim lastNumber As Double
Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click
If btnFind.Text = "Continue..." Then
btnStop.Enabled = True
btnReset.Enabled = False
btnFind.Enabled = False
btnFind.Text = "Finding..."
bgPrimeFinder.RunWorkerAsync()
Else
primeList.Items.Clear()
primeList.Items.Add("2")
primeList.Items.Add("3")
primeList.Items.Add("5")
primeList.Items.Add("7")
primeList.Items.Add("11")
btnStop.Enabled = True
btnFind.Enabled = False
btnFind.Text = "Finding..."
lastNumber = 13
bgPrimeFinder.RunWorkerAsync()
End If
End Sub
Private Sub bgPrimeFinder_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgPrimeFinder.DoWork
For testNumber As Double = lastNumber To Double.PositiveInfinity
If bgPrimeFinder.CancellationPending = False Then
If testNumber.ToString.EndsWith("0") Or testNumber.ToString.EndsWith("2") Or testNumber.ToString.EndsWith("4") Or testNumber.ToString.EndsWith("5") Or testNumber.ToString.EndsWith("6") Or testNumber.ToString.EndsWith("8") Then
'Do Nothing
Else
Dim primeNumberFlag As Boolean
For Each currentPrime As Double In primeList.Items
If currentPrime > (testNumber / 2) Then
Exit For
Else
If (testNumber / currentPrime).ToString.Contains(".") Then
primeNumberFlag = True
Else
primeNumberFlag = False
Exit For
End If
End If
Next
If primeNumberFlag = True Then
AddtoList(testNumber.ToString)
End If
End If
Else
lastNumber = testNumber
e.Cancel = True
End If
Next
End Sub
Private Sub AddtoList(ByVal [text] As String)
If primeList.InvokeRequired Then
Dim d As New AddtoListCallback(AddressOf AddtoList)
Invoke(d, New Object() {[text]})
Else
primeList.Items.Add([text])
primeList.SelectedItem = [text]
End If
End Sub