[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

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
4 Answers

Scott Seely

11/22/2002 4:48:00 AM

0

Why are you calling this service async? Since you are blocking anyway, it
really doesn't make sense.

--
Please do not send email directly to this alias. This alias is for newsgroup
purposes only.


This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cp...

"Lewis Edward Moten III" <lewis@moten.com> wrote in message
news:d78559d6.0211211903.16f2f263@posting.google.com...
> 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


(Luke Zhang [MS])

11/22/2002 12:16:00 PM

0

Hi Lewis,

What is your desired result here?


Luke

(This posting is provided "AS IS", with no warranties, and confers no
rights.)

(Luke Zhang [MS])

11/26/2002 1:53:00 PM

0

I tested your code and got following result:

The sleeper has awaken after 1Seconds!
----------------------------------------------------------------------------
----
done.The sleeper has awaken after 5Seconds!

Is this same result with yours?

Luke

(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Lewis Edward Moten III

12/26/2002 3:30:00 AM

0

"Douglas Purdy \(MS\)" <xwscom4@microsoft.com> wrote in message news:<#oBNLodkCHA.2432@tkmsftngp10>...
> Why are you calling this service async? Since you are blocking anyway, it
> really doesn't make sense.
>
> --
> Please do not send email directly to this alias. This alias is for newsgroup
> purposes only.
>
>
> This posting is provided "AS IS" with no warranties, and confers no rights.
> Use of included script samples are subject to the terms specified at
> http://www.microsoft.com/info/cp...

I'm calling it so that I can display results as they come in. Some
may take 30 seconds, others may take less then one. I would rather
the quicker ones show up first.

The service will be on seperate machine in the future and have the
ability to be called by both a stand-alone executable built by .Net
and an ASP.Net application.

What do you mean by "blocking"?