I have a category page (Classic ASP.NET) which loads products from DB (MS SQL Server). Each product (implemented as userControl) has its own databound controls. I want to load these product controls in parallel so I created a helper module which creates PageAsyncTask and register it.
Public Module AsyncHelper
Public Delegate Sub AsyncDeleg()
Private Function OnBegin(sender As Object, e As EventArgs, cb As AsyncCallback, state As Object) As IAsyncResult
Return DirectCast(state, AsyncDeleg).BeginInvoke(cb, state)
End Function
Private Sub OnEnd(ar As IAsyncResult)
Try
DirectCast(ar.AsyncState, AsyncDeleg).EndInvoke(ar)
HttpContext.Current.Trace.Write("OnEnd", ar.CompletedSynchronously)
Catch ex As Exception
DirectCast(ar.AsyncState, AsyncDeleg).Invoke()
HttpContext.Current.Trace.Write("OnEnd", ex.Message)
End Try
End Sub
Public Sub RunAsyncTask(pg As Web.UI.Page, subToRun As AsyncDeleg)
pg.RegisterAsyncTask(New PageAsyncTask(AddressOf OnBegin, AddressOf OnEnd, Nothing, subToRun, True))
End Sub
End Module
The call from the UserControl is very simple:
AsyncHelper.RunAsyncTask(Me.Page, New AsyncHelper.AsyncDeleg(AddressOf BindData))
Sub BindData queries DB and binds result to a repeater or ListView.
It works really in parallel but sometimes it produces exception like Control not found Stack empty
For this purpose catch in OnEnd works fine.
Much worse thing is that sometimes it takes part data from correct product and part from any random product without any kind of error ;(
Technical details:
.NET 4.5
httpRuntime targetFramework="4.5"
<%@ Page Async="true"
Any suggestions?
Thank you.