how do I continue code while waiting for a response

  • Thread starter Thread starter Adrian
  • Start date Start date
A

Adrian

hi
I make the following call
xmlResponse = xws.SubmitXml(Text, xmlRequest.DocumentElement,
xmlFilter.DocumentElement)

It returns what I want but I would like to do a "do while loop" while I wait
for the response 2- 10 secs so I can place some graphic or some thing to
show its not locked up...



How would I change the call to do this?



thanks
 
Adrian said:
hi
I make the following call
xmlResponse = xws.SubmitXml(Text, xmlRequest.DocumentElement,
xmlFilter.DocumentElement)

It returns what I want but I would like to do a "do while loop" while I
wait for the response 2- 10 secs so I can place some graphic or some thing
to show its not locked up...

Ok. You need a Delegate.

A Delegate is an object that wraps a function call. Among the many uses of
Delegates, is that they can be invoked either Sync or Async.

Often when invoking a Delegate you use a callback function, but you don't
have to. You can just wait for it to complete while doing other work.

Here's how:

Create a Delegate Type matching the method signature of xws.SubmitXML.
Then create a new delegate instance, refering to xws.SubmitXML.
Then call BeginInvoke, Get an IAsyncResult, do whatever, use the WaitHandle
to "listen" for completion, then call EndInvoke.

Ok, that sounds hard, but it's really not. Here's a complete working
example:

Class XYZ
Public Function SumbitXml(ByVal Text As String, _
ByVal doc As Xml.XmlElement, _
ByVal filter As Xml.XmlElement) As String
'simulate long-running method
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(10))
Return "<hello/>"
End Function
End Class
Module Module1


Private Delegate Function SubmitXmlDelegate(ByVal Text As String, _
ByVal doc As Xml.XmlElement,
_
ByVal filter As
Xml.XmlElement) As String

Sub Main()
Try
Dim xyz As New XYZ
Dim fp As New SubmitXmlDelegate(AddressOf xyz.SumbitXml)
Console.WriteLine("About to BeginInvoke")
Dim doc As Xml.XmlElement = Nothing
Dim filter As Xml.XmlElement = Nothing

'Start running funtion on a background thread. No need for a
callback function or state object.
Dim waitObject As IAsyncResult = fp.BeginInvoke("", doc, filter,
Nothing, Nothing)

'Wait for it to complete
Do
Console.WriteLine("Waiting...")
Loop While Not
waitObject.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(1), False)

'Now we know execution is complete since WaitHandle.WaitOne
returned True
Dim rv As String = fp.EndInvoke(waitObject)

Console.WriteLine("Completed: " & rv)

Catch ex As Exception
Console.WriteLine(ex)

End Try

End Sub


End Module
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top