Understanding Asychronous Operations

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have recently implemented asychronous httpwebrequest. My goal in doing
this was to allow me to move my form around while a download was occuring
and to keep the form from being distored when other windows are moved over it
during an httpwebrequest operation. Well, I am running asychronous but I
have not achieved my goals. I have included my code, I hope someone might
give me a suggestion.

Function GetImageFromURL(ByVal url As String) As Byte()
allDone.Reset()
BusyFlag = True
Try
XMainForm.StatusMessage.Clear()
XMainForm.StatusMessage.AppendText("* Loading the file from the server"
Dim wr As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
If Not ProxyObject Is Nothing Then wr.Proxy = ProxyObject
Dim MRS As New RequestState
MRS.request = wr
Dim result As IAsyncResult = CType(wr.BeginGetResponse(AddressOf
RespCallBack, MRS), IAsyncResult)
allDone.WaitOne() 'shouldn't i be able to move my form around while in
wait mode
MRS.response.Close()
XMainForm.StatusMessage.AppendText("* File download complete. Preparing
display.")
BusyFlag = False
Return MRS.ReturnBLOB

Catch MyException As Exception
XMainForm.StatusMessage.AppendText(vbCrLf)
XMainForm.StatusMessage.AppendText(vbCrLf & "* Unable to load the requested
file due to :" & MyException.Message)
End Try
 
Nope, your WaitOne assumption is your hang-up... I recommend using gated
booleans. Or at least that is how I write my own code, such that:

// In your class
private bool inRequest = false;
private readonly object requestLock = new object();

// In your firing method
lock(requestLock) { if ( inRequest ) { return; } inRequest = true; }
// Fire your request
// Let execution fall through

// In your response callback
// End Your Request and processing
lock(requestLock) { inRequest = false; }
UpdateUIComplete();

The above is in C#, but you have a SyncLock in VB that is the same as lock in
C#.
When you update your UI after the request is complete you can implement that to
a
BeginInvoke call to run your UI update code on the same thread as the UI.

The topics above are basic synchronization and re-entry protection, but
hopefully
they give you what you need.
 
My calling sequence is a function that returns the results of a webrequest.
It uses an asychronous callback to fetch the data. So I have to wait for
the completion of the callback before allowing my function to return a value.
So synclock is confusing to me. It prevents multiple threads from accessing
the same call back routine. I don't mind waiting for my webrequest to
complete. What I don't like is that my applicaiton window will not respond
to events like click and drag while its waiting for data. Also, while waiting
for data if I drag another window over my application, the application window
becomes poluted by the window that passed over it.

I thought asychronous operation would allow my window to respond to external
events.

Fred
 
Back
Top