Problem accessing methods in async socket code

hi all,

I have a problem whereby if i try and access a method in a async thread it freezes my whole program or goes into a endless loop or something :s

I greatly appreciate any help/advice.

public bool StartAsync()
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(server.hostpath);
allDone = new ManualResetEvent(false);
buffer = new byte[1024];
OutFile = new FileStream("C:\\My save file", FileMode.Create);
writeEvent = new AutoResetEvent(true);

request.Method = "GET";

// Begin to get response
request.BeginGetResponse(new AsyncCallback(ResponseCallBack), request);

// Wait until the response is finished
allDone.WaitOne();

// Close the filestream
OutFile.Close();

return true;
}

private void ResponseCallBack(IAsyncResult ar)
{
HttpWebRequest request = (HttpWebRequest)ar.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(ar);

// ========> HERE IS THE PROBLEM
// Trying to access response methods here causes the ****ing thing to freeze
// i.e long fileLength = response.ContentLength;

Stream strm = response.GetResponseStream();

strm.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(ReadCallBack), strm);
}

private void ReadCallBack(IAsyncResult ar)
{
Stream strm = (Stream)ar.AsyncState;
int count = strm.EndRead(ar);

if (count > 0)
{
writeEvent.WaitOne();
OutFile.Write(buffer, 0, count);
writeEvent.Set();
strm.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(ReadCallBack), strm);
}
else
{
strm.Close();
allDone.Set();
}
}
[1779 byte] By [feral] at [2007-11-20 11:49:25]
# 1 Re: Problem accessing methods in async socket code
That line shouldn't cause your program to freeze. Wrap your code in a try/catch and see if there's an exception being thrown somewhere.
Mutant_Fruit at 2007-11-9 11:37:04 >
# 2 Re: Problem accessing methods in async socket code
thanks for the reply,

I checked and there were no exceptions, but it occured to me that the methods were not yet ready since I had not read the HTTP header!!

So now I am accessing the methods after the http header is read ;> (i.e after the first 1024 bytes of the response.
feral at 2007-11-9 11:38:04 >