G
Glen
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, the callback routine 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.Threading
Public Class AsynchTest1
Inherits System.Web.UI.Page
Protected WithEvents btnGo As System.Web.UI.WebControls.Button
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.EventArgs) Handles MyBase.Load
End Sub
------Button to start process
Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnGo.Click
Session("StartProc") = Today.Now
Dim TestDlgt As TestDelegate = New TestDelegate(AddressOf
TestRoutine)
Dim ar As IAsyncResult = TestDlgt.BeginInvoke(New
AsyncCallback(AddressOf TestComplete), _
Nothing)
lblStatus.Text = "Procedure started..."
End Sub
-------Asynchronous Routine
Private Function TestRoutine() As String
Dim start As DateTime
SyncLock Session.SyncRoot
start = Session("StartProc")
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(ByVal ar As IAsyncResult)
Dim ad As TestDelegate = CType(ar.AsyncState, TestDelegate)
lblStatus.Text = ad.EndInvoke(ar)
lblStatus.ForeColor = Color.Red
End Sub
End Class
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, the callback routine 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.Threading
Public Class AsynchTest1
Inherits System.Web.UI.Page
Protected WithEvents btnGo As System.Web.UI.WebControls.Button
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.EventArgs) Handles MyBase.Load
End Sub
------Button to start process
Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnGo.Click
Session("StartProc") = Today.Now
Dim TestDlgt As TestDelegate = New TestDelegate(AddressOf
TestRoutine)
Dim ar As IAsyncResult = TestDlgt.BeginInvoke(New
AsyncCallback(AddressOf TestComplete), _
Nothing)
lblStatus.Text = "Procedure started..."
End Sub
-------Asynchronous Routine
Private Function TestRoutine() As String
Dim start As DateTime
SyncLock Session.SyncRoot
start = Session("StartProc")
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(ByVal ar As IAsyncResult)
Dim ad As TestDelegate = CType(ar.AsyncState, TestDelegate)
lblStatus.Text = ad.EndInvoke(ar)
lblStatus.ForeColor = Color.Red
End Sub
End Class