Oct 09 2022 05:14 PM
I have a problem using Parallel.ForEachAsync.
I have a list of 3500 url's, and associated data. They are stored in "links" which is a List(Of link).I wish to simply validate each url to confirm they exist. I would like to exploit parallelism and test them (say) 10 at a time. I have developed the following code, but I cannot get it to work.
At the statement "Async Sub(t)" I get a BC36670 error, and nothing I can do seems to clear it. I'm new to parallelism, and I can't understand what the error message is trying to tell me. Any insights?
httpclient.DefaultRequestHeaders.Clear()
Try
count = 0
Dim options As New ParallelOptions()
options.MaxDegreeOfParallelism = 10
Await Parallel.ForEachAsync(links, options,
Async Sub(t)
count += 1
'AppendText(TextBox2, $"Processing index {t.index}{vbCrLf}")
If t.url = "" Then
t.status = "<font color='orange'>Missing</font>"
Missing += 1
Else
'Dim r = Await TestURL(t.url)
Dim httpResult = Await httpclient.GetAsync(t.url)
Dim r = httpResult.IsSuccessStatusCode
t.OK = r
If r Then
t.status = "<font color='green'>OK</font>"
OK += 1
Else
t.status = "<font color='red'>Not found</font>"
NotFound += 1
End If
SetText(TextBox1, $"Processed {count}/{links.Count}")
End If
End Sub)
Catch ex As Exception
MsgBox(ex.Message & vbCrLf & ex.StackTrace, vbCritical + vbOK, "Parallel web access failed")
End Try
' Produce results
logWriter.WriteLine("<table border=1>")
logWriter.WriteLine("<tr><th>WWFFID</th><th>Name</th><th>State</th><th>HTTPLink</th><th>Status</th></tr>")
For Each lnk In links
logWriter.WriteLine($"<tr><td>{lnk.WWFFID}</td><td>{lnk.LongName}</td><td>{lnk.State}</td><td><a href='{lnk.url}'>{lnk.url}</a></td><td>{lnk.status}</td></tr>")
Next
logWriter.WriteLine("</table>")
logWriter.WriteLine($"<br>{links.Count} total links validated. {OK} OK, {Missing} Missing, {NotFound} Not Found")
logWriter.Close()
SetText(TextBox1, "Done")
End Using
Return True
End Function
Public Class link
Property index As Integer
Property url As String
Property WWFFID As String
Property LongName As String
Property State As String
Property status As String
Property OK As Boolean
Sub New(index As String, url As String, WWFFID As String, LongName As String, State As String)
_index = index
_url = url
_WWFFID = WWFFID
_LongName = LongName
_State = State
_status = status
_OK = False
End Sub
End Class
O
Oct 09 2022 07:33 PM
Hi @vk3ohm,
Thanks for posting your issue here.
However this platform is used for how-to discussions and sharing best practices for building any app with .NET.Since your issue is a technical question, welcome to post it in Microsoft Q&A forum, the support team and communities on Microsoft Q&A will help you for any technical questions.
Besides, it will be appreciated if you can share it here once you post this technical question Microsoft Q&A.
Best Regards,
Lan Huang