Lewis Edward Moten III
12/26/2002 3:30:00 AM
I'm running into a few problems with Asynchronouse calls with ASP.Net
and webservices. My target solution is to be able to search multiple
websites asynchronously using webservices - and displaying the results
on an ASP.Net page as they come in.
For a working example, I decided to start small and work my way up.
Rite now I created a simple web service with a method called sleep
that loops until the specified number of seconds have passed.
-----------------------------------------------------------------------
Imports System.Web.Services
<WebService(Namespace:="http://localhost/sleeper/")> _
Public Class SleeperService
Inherits System.Web.Services.WebService
#Region " Web Services Designer Generated Code "
... <snip> ...
#End Region
' WEB SERVICE EXAMPLE
' The HelloWorld() example service returns the string Hello World.
' To build, uncomment the following lines then save and build the
project.
' To test this web service, ensure that the .asmx file is the
start page
' and press F5.
'
<WebMethod()> Public Function Sleep(ByVal Seconds As Long) As
String
Dim Start As DateTime = Now
While Not DateDiff(DateInterval.Second, Start, Now) = Seconds
' do nothing
End While
Return "The sleeper has awaken after " & Seconds.ToString & "
seconds!"
End Function
End Class
-----------------------------------------------------------------------
This web service runs fine. I've added it as a web reference to my
project and created a page that calls it asynchronously. However,
when the page runs, it appears to go on forever. I have a loop that
continues to wait until the web services complete. It is my
understanding that the problem may be due to the fact that nothing is
really being processed while my code loops and takes all the CPU
resources, thus preventing my service to have enough CPU to process
itself.
In visual basic, I would normally use "DoEvents". Rite now this is
only available through windows forms:
System.Windows.Forms.Application.DoEvents
Does anyone know of a substitute to use in ASP.Net webpages to replace
DoEvents? Here is my code for the aspx test page:
-----------------------------------------------------------------------
Public Class TestService
Inherits System.Web.UI.Page
Public Sleeper1 As localhost.SleeperService = New
localhost.SleeperService()
Public Awaken1 As AsyncCallback = New AsyncCallback(AddressOf
WakeUp1)
Public WokeUp1 As Boolean = False
Public Ar1 As IAsyncResult
Public Sleeper2 As localhost.SleeperService = New
localhost.SleeperService()
Public Awaken2 As AsyncCallback = New AsyncCallback(AddressOf
WakeUp2)
Public WokeUp2 As Boolean = False
Public Ar2 As IAsyncResult
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form
Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Sleeper1.Timeout = 5000
Sleeper2.Timeout = 5000
Ar1 = Sleeper1.BeginSleep(5, Awaken1, Sleeper1)
Ar2 = Sleeper2.BeginSleep(1, Awaken2, Sleeper2)
While Not (Ar1.IsCompleted And Ar2.IsCompleted)
If Not Ar1.IsCompleted Then
Ar1.AsyncWaitHandle.WaitOne(1000, False)
If Not Ar2.IsCompleted Then
Ar2.AsyncWaitHandle.WaitOne(1000, False)
End While
Response.Write("<HR>done.")
End Sub
Sub WakeUp1(ByVal ar As IAsyncResult)
Dim Message As String
Sleeper1 = ar.AsyncState
Message = Sleeper1.EndSleep(ar)
Response.Write(Message & "<BR>")
Response.Flush()
WokeUp1 = True
End Sub
Sub WakeUp2(ByVal ar As IAsyncResult)
Dim Message As String
Sleeper2 = ar.AsyncState
Message = Sleeper2.EndSleep(ar)
Response.Write(Message & "<BR>")
Response.Flush()
WokeUp2 = True
End Sub
End Class