|
-
January 30th, 2012, 01:41 PM
#1
WebRequest.BeginGetResponse Problems...
So I am trying to implement a Async HttpWebRequest/HttpWebResponse call on a Background Thread apart from the Main UI Thread.
Although everytime, the Response is "called-back", for some reason it uses the UI thread, is there anyway to not let this happen.
Basically, when I run the Async calls, the actual methods are free from the UI, except the actual calling of the delegate methods, which cause my mouse pointer to show the "loading/progress" cursor...
Ex.
Code:
private ManualResetEvent allDone = new ManualResetEvent(false);
private void button1_Click(object bsender, EventArgs bve)
{
Thread t = new Thread(new ThreadStart(delegate()
{
string url = "http://example/"
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "multipart/form-data; boundary=" + Config.REQUEST_BOUNDARY;
req.BeginGetRequestStream(GetRequestStreamCallback, req);
allDone.WaitOne();
Console.WriteLine("Request/Response Callback is Done...");
}));
t.IsBackground = true;
t.Start();
}
private void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
//Add data to request stream...
//This line seems to be the culprit of making my UI thread hang for a second...
request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
}
private void GetResponseCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
Stream streamResponse = response.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse);
string responseString = streamRead.ReadToEnd();
Console.WriteLine("Response Callback: " + responseString);
streamResponse.Close();
streamRead.Close();
response.Close();
allDone.Set();
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|