Hi i have been struggling with this for a few days now. I am attempting to send an xml string to a http address multiple times and receive an xml response from the site. I have so far managed to construct the following module that handles the asynchronous post.
called by the following procedure
So for testing i am
1 - clicking a button which is firing the above procedure.
2 - sending the xml using the async post
3 - receiving an xml string
4 - appending the string to a txt file.
Now i would have thought i could hit the button a few 100 times and these would be qued into nthe threadpool and the responses handled as they are returned. However as soon as i click the button the UI freezes and is non responsive until the complete process has completed.
One way to get around this is to use multiple background workers to run the procedure but i would be looking to run about 25 + of them and i dont see the point as i am already using threadpool.
can anyone suggest how to resolve this problem.
Thanks in advance.
VB.NET:
Imports System.Net
Imports System.IO
Imports System.Threading
Imports System.Collections
Module Module1
Public Class RequestState
Public Request As HttpWebRequest
Public URL As String
Public Sub New()
Request = Nothing
End Sub
End Class
Public Sub CreateAsyncWebRequest(ByVal MyURL As String, ByVal XMLInputString As String)
Dim oHttp As HttpWebRequest = DirectCast(WebRequest.Create(MyURL), HttpWebRequest)
oHttp.Method = "POST"
Dim postBuffer As Byte() = System.Text.Encoding.ASCII.GetBytes(XMLInputString)
oHttp.ContentLength = postBuffer.Length
Dim postData As Stream = oHttp.GetRequestStream()
postData.Write(postBuffer, 0, postBuffer.Length)
postData.Close()
Dim objState As New RequestState
objState.Request = oHttp
objState.URL = MyURL
Dim result As IAsyncResult = DirectCast(oHttp.BeginGetResponse(New AsyncCallback(AddressOf ProcessAsyncWebResponse), objState), IAsyncResult)
ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, _
New WaitOrTimerCallback(AddressOf ScanTimeoutCallback), _
objState, (30 * 1000), True)
End Sub
Private Sub ScanTimeoutCallback(ByVal Request As Object, ByVal TimedOut As Boolean)
If TimedOut Then
Dim objState As RequestState = DirectCast(Request, RequestState)
If Not (Request Is Nothing) Then
objState.Request.Abort()
Console.WriteLine(objState.URL + " is processed")
End If
End If
End Sub
Private Sub ProcessAsyncWebResponse(ByVal Result As IAsyncResult)
Dim objState As RequestState = DirectCast(Result.AsyncState, RequestState)
Dim oHttp As HttpWebRequest = DirectCast(objState.Request, HttpWebRequest)
Dim myResponse As HttpWebResponse
Dim loResponseStream As StreamReader
Dim strxmlout As String
Try
myResponse = DirectCast(oHttp.EndGetResponse(Result), HttpWebResponse)
loResponseStream = New StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.ASCII)
strxmlout = loResponseStream.ReadToEnd()
'rtbSendNotes.Text = m_xmlOut
'frmMainProcess.TextBox2.Text += strxmlout
writelog.writelogfile(objState.URL + " is processed-i win -------" + strxmlout)
Catch ex As Exception
'Exception handling code
writelog.writelogfile(objState.URL + " is processed with exception: " + ex.Message)
Finally
If Not (myResponse Is Nothing) Then
myResponse.Close()
End If
If Not (loResponseStream Is Nothing) Then
loResponseStream.Close()
End If
End Try
frmMainProcess.TextBox2.Refresh()
End Sub
End Module
called by the following procedure
VB.NET:
CreateAsyncWebRequest(My.Settings.MPIURL, strxmlreq)
So for testing i am
1 - clicking a button which is firing the above procedure.
2 - sending the xml using the async post
3 - receiving an xml string
4 - appending the string to a txt file.
Now i would have thought i could hit the button a few 100 times and these would be qued into nthe threadpool and the responses handled as they are returned. However as soon as i click the button the UI freezes and is non responsive until the complete process has completed.
One way to get around this is to use multiple background workers to run the procedure but i would be looking to run about 25 + of them and i dont see the point as i am already using threadpool.
can anyone suggest how to resolve this problem.
Thanks in advance.