Asynchronous long-running tasks in ASP.NET application

I  had a requirement to have asynchronous background tasks(i,e. calls to different web services) that should be shown by ASP.NET web page, when completed.
The approach is described in How To: Submit and Poll for Long-Running Tasks (see also wiki copy).
Instead of using minimal TempDataStore from the article, I used   Session Data Management Tool created by Xiangyang Liu  (see also my code of  Configuration class). Note that in Asyncronous Callback functions I wasn’t able to access ASP.Net Session state.

I’ve posted some snippets from the code below(published using CopySourceAsHtml add-in) :



                    Dim dlgtDoSearch As New DoSearchDelegate(AddressOf wrp.doSearch)

                    Dim stateAsync As New AsyncSearchResultsState(Session.SessionID, sDsName)


                    Dim acb As New AsyncCallback(AddressOf SearchResultsAsyncCallBack)

                    dlgtDoSearch.BeginInvoke(queryState, maxResultsPerPage, PageNumber, acb, stateAsync)




        Public Sub SearchResultsAsyncCallBack(ByVal ar As IAsyncResult)

            Dim ard As AsyncResult = CType(ar, AsyncResult)

            Dim dlgtDoSearch As DoSearchDelegate = CType(ard.AsyncDelegate,  DoSearchDelegate)

            Dim result As Object ‘possible types – DataSet or Exception


                result = dlgtDoSearch.EndInvoke(ar)

            Catch exc As Exception

                result = exc

            End Try

            StoreInSessionManager(ar, result)

        End Sub ‘SearchResultsAsyncCallBack

        ‘//Dim result As Object ‘possible types – DataSet or Exception

        Public Sub StoreInSessionManager(ByVal ar As IAsyncResult, ByVal result As Object)

            ‘store the values in the data store

            Dim myState As AsyncSearchResultsState = CType(ar.AsyncState, AsyncSearchResultsState)

            Dim mgr As New SessionLib.SessionManager ‘required reference and CodeProjectSessionToolsSessionobjDebugSessionManager.dll

            DebugHelper.TracedLine((” Session “ + myState.SessionID + ” Element is “ + myState.m_sElementKey + ” ResultType=” + result.GetType.ToString()))

            mgr.SetSessionData(myState.SessionID, myState.m_sElementKey, result)

        End Sub ‘StoreInSessionManager

        ‘approach from “Submit and Poll for Long-Running Tasks” Poll.aspx/Results.aspx        ‘

        Sub PollSearchResults()

m_bRefreshRequired = False

            Dim mgr As New SessionLib.SessionManager ‘alternatively use Asynchronous Invocation Application Block

            Dim res1 As Object = mgr.GetSessionData(Session.SessionID, sDsName)

            DebugHelper.TracedLine(” Session “ + Session.SessionID + ” Element is “ + sDsName)

            If (Not IsNothing(res1)) Then

                ‘long task is complete, goto the results

                    If TypeOf res1 Is SearchResultsDatasetBase Then

                        ‘Only the first time when the search results are polled, poll is required

                        ‘The simplest way is to check if sessnDataset.Dataset is null or not,

                        ‘TODO: may be check at the beginning and exit sub immediately

                        ‘Alternatively I can save some flag to SearchResultsDatasetBase (or save special type to sessionManager)

                        ‘that will indicate that reading is done

                        If IsNothing(sessnDataset.Dataset) Then


                        End If

                    ElseIf TypeOf res1 Is Exception Then

                        BaseResultItemsListUsc.LogWarningAndShowError(listResults, res1)


                        Throw New ExceptionWithDetails(“Unexpected Asynchronous results type returned”, res1.GetType.ToString)

                    End If

                Catch ex As Exception

                    BaseResultItemsListUsc.LogWarningAndShowError(listResults, AppEventLogName, ex, resType, queryState)


                    If Not listResults Is Nothing Then

                        listResults.LabelCaption.Visible = True

                    End If

                End Try


                    m_bRefreshRequired = True

            End If


            If m_bRefreshRequired = True Then

                JScriptHelper.SetRefresh(Page, 5)

            End If

        End Sub

    public class AsyncSearchResultsState


        // Member variables

        public string m_sessionID;

        public string m_sElementKey =“”;

        //        public System.Web.SessionState.HttpSessionState m_SessionState=null;


        public AsyncSearchResultsState(string sessionID,string ElementKey) //,HttpSessionState sessionState)


            m_sessionID = sessionID;



        /// <summary>

        /// Property SessionID (string)

        /// </summary>

        public string SessionID




                return this.m_sessionID;




                this.m_sessionID = value;