This is my first attempt as asynchronous processing. I have created a
small test app as proof of concept, but I am having one proglem. In
the example (code listed below), my callback routine has two problems:
1. It runs TWICE; and
2. While it seems to update the web page controls, the results never
show up on the page.
I am using delegates per a couple of examples I found on MSDN and
elsewhere.
What I'm trying to accomplish is to spawn an asynchronous routine from
the page. When the routine is complete and the callback routine
executes, it should update the screen to let the user know it's done.
Any help would be greatly appreciated. My code is listed below.
Sincerely,
Glen Wolinsky
--------------------------
Numbers 6:24-26
--------------------------
-------------------------------------------------------------------------------------------------------------
Imports System.Threadin g
Public Class AsynchTest1
Inherits System.Web.UI.P age
Protected WithEvents btnGo As System.Web.UI.W ebControls.Butt on
Public Delegate Function TestDelegate() As String
Dim intTemp As Integer
------Page Load
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
End Sub
------Button to start process
Private Sub btnGo_Click(ByV al sender As System.Object, ByVal e As
System.EventArg s) Handles btnGo.Click
Session("StartP roc") = Today.Now
Dim TestDlgt As TestDelegate = New TestDelegate(Ad dressOf
TestRoutine)
Dim ar As IAsyncResult = TestDlgt.BeginI nvoke(New
AsyncCallback(A ddressOf TestComplete), _
Nothing)
lblStatus.Text = "Procedure started..."
End Sub
-------Asynchronous Routine
Private Function TestRoutine() As String
Dim start As DateTime
SyncLock Session.SyncRoo t
start = Session("StartP roc")
End SyncLock
Do While (Date.Now.Ticks - start.Ticks) < 100000000
'Process this loop for a few seconds
Loop
Return "The asynchronous process is complete"
End Function
------Completion callback routine
Private Sub TestComplete(By Val ar As IAsyncResult)
Dim ad As TestDelegate = CType(ar.AsyncS tate, TestDelegate)
lblStatus.Text = ad.EndInvoke(ar )
lblStatus.ForeC olor = Color.Red
End Sub
End Class