Convert a Sub that runs a Linq Query to an Async Sub

  • Thread starter Thread starter Paul D Goldstein
  • Start date Start date
P

Paul D Goldstein

Guest
I have a Sub that builds multiple datagridviews from Linq queries.
The datagridviews are contained in Panels. There are 42 panels representing the possible days in a Calendar grid.
The routine builds a Datatable for the particular Month being displayed, and then adds those records to the datagridview of the corresponding panel.
I’d like the entire routine to run asynchronously since the values loaded in the datagridviews are one of many informational areas on the form.

Here is the code for the Sub:

Private Sub subLoadDGVs()

Dim strSQL As String
Dim daBMList As New OleDbDataAdapter
Dim dtBMList As New DataTable
Dim intPanelIX As Integer
Dim pnlCurrent As Panel
Dim dgvCurrent As DataGridView

' Add BMList Dates to Each Calendar
strSQL = "SELECT tblBMList.RecordKey, tblBMList.FirstName, tblBMList.LastName, tblBMList.BMDate, tblBMList.BMLocation "
strSQL = strSQL & "FROM tblBMList "
strSQL = strSQL & "WHERE tblBMList.InActiveFL=False AND tblBMList.BMDate BETWEEN #" & Format(dteStartOfPanel, "MM/dd/yyyy") & "# AND #" & Format(dteEnd, "MM/dd/yyyy") & "#"
daBMList = New OleDbDataAdapter(strSQL, myConnection)
daBMList.Fill(dtBMList)

Dim qryBMList = From BMListRecord In dtBMList.AsEnumerable
Order By BMListRecord.Field(Of Date)("BMDate"),
BMListRecord.Field(Of String)("LastName"),
BMListRecord.Field(Of String)("FirstName")
Select New With {
.RecordKey = BMListRecord.Field(Of Integer)("RecordKey"),
.FirstName = BMListRecord.Field(Of String)("FirstName"),
.LastName = BMListRecord.Field(Of String)("LastName"),
.BMDate = BMListRecord.Field(Of Date)("BMDate")
}

For Each BMListRecord In qryBMList
intPanelIX = DateDiff(DateInterval.Day, dteStartOfPanel, BMListRecord.BMDate) + 1
pnlCurrent = Me.flpCalendar.Controls("panl" & Format(intPanelIX, "00"))
dgvCurrent = pnlCurrent.Controls("dgvOtherInfo" & Format(intPanelIX, "00"))
dgvCurrent.Rows.Add(Format(BMListRecord.BMDate, "Short Time") & " " & BMListRecord.FirstName & " " & BMListRecord.LastName)
dgvCurrent.Rows(dgvCurrent.Rows.Count - 1).DefaultCellStyle.ForeColor = Color.Maroon
dgvCurrent.ClearSelection()
Next

daBMList = Nothing
dtBMList.Clear()
Call subAddOutlookAppts()

End Sub

I tried converting the Sub to an Async by changing the declartion to:

Private Async Sub subLoadDGVs()

and substituting the

For Each BMListRecord In qryBMList
…<DGV build logic>…
Next

loop with:

Parallel.ForEach(qryBMList, Sub(BMListRecord)
…<same logic in original loop>…
End Sub)

but I’m still getting a warning on the declaration line of the Sub:
BC42356: This Async method lacks 'Await' operators and so will run synchronously. Consider using the 'Await' operator to await non-blocking API calls, 'Await.Task.Run(…) to do CPU-bound work on a background thread.

Any ideas would be greatly appreciated.

Sincerly,
Paul Goldstein


Paul D. Goldstein Forceware Systems, Inc.

Continue reading...
 
Back
Top