Problem accessing methods in async socket code
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();
}
}

