Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Asynchronous webrequest problem

Asynchronous webrequest problem

Scheduled Pinned Locked Moved C#
csharpdesignsysadminhelp
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    wajih boukaram
    wrote on last edited by
    #1

    Hi I began writing a c# program and everything was going ok until i tried to use asynchronous webrequests instead of synchronous ones (since the UI would be unresponsive while it was fetching the response) and i have a slight problem: the user signs in by clicking a button on the form - the program fetches the data from the server asynchronously - the problem is with the callback function - when debugging i see that the request was successfully completed but when i try to access any controls (like filling a textbox with the response) from the callback function it just stop executing the function - no exceptions, no errors - i can still interact with the program but it just wont alter anything to do with the form's controls (the form's data members however were being altered with no issues - a string and an integer to be exact) I'm a newbie at c# and i just started using the asynchronous method so if im doing sumthing wrong please advise thank you for your time

    C 1 Reply Last reply
    0
    • W wajih boukaram

      Hi I began writing a c# program and everything was going ok until i tried to use asynchronous webrequests instead of synchronous ones (since the UI would be unresponsive while it was fetching the response) and i have a slight problem: the user signs in by clicking a button on the form - the program fetches the data from the server asynchronously - the problem is with the callback function - when debugging i see that the request was successfully completed but when i try to access any controls (like filling a textbox with the response) from the callback function it just stop executing the function - no exceptions, no errors - i can still interact with the program but it just wont alter anything to do with the form's controls (the form's data members however were being altered with no issues - a string and an integer to be exact) I'm a newbie at c# and i just started using the asynchronous method so if im doing sumthing wrong please advise thank you for your time

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      A good first step would be to post some code, so we can offer some advice on it.

      Christian Graus - C++ MVP

      W 1 Reply Last reply
      0
      • C Christian Graus

        A good first step would be to post some code, so we can offer some advice on it.

        Christian Graus - C++ MVP

        W Offline
        W Offline
        wajih boukaram
        wrote on last edited by
        #3

        Hi thanks for your quick reply these are methods of my MainForm class (base class is Form) private void getResult(String url, AsyncCallback callback) { try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; if(useCompression) request.Headers.Add("Accept-Encoding: gzip,deflate"); request.CookieContainer = mainCookie; request.AllowAutoRedirect = true; request.AllowWriteStreamBuffering = true; IAsyncResult result = request.BeginGetResponse(callback, request); } catch (WebException wex) { MessageBox.Show("Error Connecting: Check your connection settings...\nDetails: " + wex.Status.ToString()); return; } } private void SignInButton_Click(object sender, EventArgs e) { DisableSignInControls(); String url = mirror + "signin.aspx?user=" + UserNameComboBox.Text + "&pass=" + PasswordTextBox.Text; mainCookie = new CookieContainer(); getResult(url, new AsyncCallback(signIn)); } private void signIn(IAsyncResult result) { HttpWebRequest request = (HttpWebRequest)result.AsyncState; HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result); String responseResult = (useCompression ? decompressResponse(response) : new StreamReader(response.GetResponseStream()).ReadToEnd()); int i = responseResult.IndexOf("<response>"); if (i < 0) MessageBox.Show("Corrupt response. Please try again."); else { int j = responseResult.IndexOf("</response>"); String temp = responseResult.Substring(i + 10, j - i - 10); if (temp != "") { String[] returnValues = temp.Split('|'); if (returnValues.Length == 1) MessageBox.Show(returnValues[0]); else { sid = returnValues[0]; num_messages = int.Parse(returnValues[1]); MessageNumber.Text = "Messages: Left " + num_messages; SignInGroupBox.Visible = fal

        C 1 Reply Last reply
        0
        • W wajih boukaram

          Hi thanks for your quick reply these are methods of my MainForm class (base class is Form) private void getResult(String url, AsyncCallback callback) { try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; if(useCompression) request.Headers.Add("Accept-Encoding: gzip,deflate"); request.CookieContainer = mainCookie; request.AllowAutoRedirect = true; request.AllowWriteStreamBuffering = true; IAsyncResult result = request.BeginGetResponse(callback, request); } catch (WebException wex) { MessageBox.Show("Error Connecting: Check your connection settings...\nDetails: " + wex.Status.ToString()); return; } } private void SignInButton_Click(object sender, EventArgs e) { DisableSignInControls(); String url = mirror + "signin.aspx?user=" + UserNameComboBox.Text + "&pass=" + PasswordTextBox.Text; mainCookie = new CookieContainer(); getResult(url, new AsyncCallback(signIn)); } private void signIn(IAsyncResult result) { HttpWebRequest request = (HttpWebRequest)result.AsyncState; HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result); String responseResult = (useCompression ? decompressResponse(response) : new StreamReader(response.GetResponseStream()).ReadToEnd()); int i = responseResult.IndexOf("<response>"); if (i < 0) MessageBox.Show("Corrupt response. Please try again."); else { int j = responseResult.IndexOf("</response>"); String temp = responseResult.Substring(i + 10, j - i - 10); if (temp != "") { String[] returnValues = temp.Split('|'); if (returnValues.Length == 1) MessageBox.Show(returnValues[0]); else { sid = returnValues[0]; num_messages = int.Parse(returnValues[1]); MessageNumber.Text = "Messages: Left " + num_messages; SignInGroupBox.Visible = fal

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          wajih.boukaram wrote:

          IAsyncResult result = request.BeginGetResponse(callback, request);

          Doesn't result just get discarded ? And where is the callback hooked up to a method ?

          Christian Graus - C++ MVP

          W 1 Reply Last reply
          0
          • C Christian Graus

            wajih.boukaram wrote:

            IAsyncResult result = request.BeginGetResponse(callback, request);

            Doesn't result just get discarded ? And where is the callback hooked up to a method ?

            Christian Graus - C++ MVP

            W Offline
            W Offline
            wajih boukaram
            wrote on last edited by
            #5

            yea it does but all that matters is the callback function (which i assigned when i called getResult(url, new AsyncCallback(signIn)); so its hooked up to signIn it works fine the way it is - only problem is changing the form's control values - i think it might have something to do with threading because if i try calling the function a second time (I hate c# sometimes) it says sumthin about accessing an object that was not created on the same thread - when i first tried to do this asynchronously i actually just created a new thread that called my initial signIn function which was just a regular function and not a callback - i got the same error message when acessing the controls. any suggestions? thanks for your replies.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups