CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2010
    Posts
    3

    Exclamation 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();
    }

  2. #2
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: WebRequest.BeginGetResponse Problems...

    Moved from c# to asp.net (but with a 1 month link in c# forum) as I do believe here you should find a better help
    Last edited by Cimperiali; January 30th, 2012 at 03:55 PM.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  3. #3
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: WebRequest.BeginGetResponse Problems...

    by the way, even if this is coded in VB, the concept of a secondary thread thhat should get the callback seems what you're looking for:
    http://msdn.microsoft.com/en-us/magazine/cc188732.aspx
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  4. #4
    Join Date
    Jul 2010
    Posts
    3

    Re: WebRequest.BeginGetResponse Problems...

    All I am wondering is why does the WebResponse Callback always use the UI thread...

    And is there a way to stop this from happening?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured