Question

I have been working on this a couple of days looking for an answer on the internet and there doesn't seem to be a good one for compact framework.

What I need to do is get the response string from GetResponseCallBack and pass it back to the main thread to update the GUI.

Has anyone done this in compact framework? I am using .NET 3.5

Here are my two callbacks

private static void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
    HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;

    // End the operation
    Stream postStream = request.EndGetRequestStream(asynchronousResult);


    string postData = "&location=funville";

    // Convert the string into a byte array. 
    byte[] byteArray = Encoding.UTF8.GetBytes(postData);

    // Write to the request stream.
    postStream.Write(byteArray, 0, postData.Length);
    postStream.Close();

    // Start the asynchronous operation to get the response
    request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
}

private static void GetResponseCallback(IAsyncResult asynchronousResult)
{
    HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;

    // End the operation
    HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
    Stream streamResponse = response.GetResponseStream();
    StreamReader streamRead = new StreamReader(streamResponse);
    string responseString = streamRead.ReadToEnd();
    // Close the stream object
    streamResponse.Close();
    streamRead.Close();

    // Release the HttpWebResponse
    response.Close();
    allDone.Set();
}

Here is the code I use to create the request

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.SendChunked = true;
request.ContentType = "application/x-www-form-urlencoded";

// Set the Method property to 'POST' to post data to the URI.
request.Method = "POST";

// start the asynchronous operation
request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);

// Keep the main thread from continuing while the asynchronous 
// operation completes. A real world application 
// could do something useful such as updating its user interface. 
allDone.WaitOne();

It is mainly just the default code from MSDN, and I am getting the correct response data because I had a MessageBox pop the response string in the call back I just need some direction on passing it to the main thread.

Was it helpful?

Solution

You already have code that will notify the main thread using the allDone event. You can put the result in a variable that's visible to the main thread and then call allDone.Set. When the main thread sees that the event is set, it can then get the result from that variable.

Your primary problem appears to be how to determine if the result is ready. That's a bit of a harder problem and depends on what the main thread is doing. If the main thread is waiting for user input, then you could replace the event and global variable with an event-based asynchronous pattern. The key would be to marshal the event to the UI thread using something like Control.Invoke.

If your main thread is doing something else, then it will have to poll the allDone event from time to time. Something like:

while (!allDone.WaitOne(0))
{
    // do stuff
}
// get and process result

Or:

while (true)
{
    if (allDone.WaitOne(0))
    {
        // get and process result
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top