Caner
Caner

Reputation: 873

Web Request asynchronous iscompleted

I will be use this. http://www.matlus.com/httpwebrequest-asynchronous-programming/

But how can I know request iscompleted? So how do I know that the work done?

I use like it :

PostAsync(url, postParameters, callbackState =>
{
    if (callbackState.Exception != null)
    {
        throw callbackState.Exception;
    }
    else
    {
        WriteCallBack(GetResponseText(callbackState.ResponseStream));
    }
});

But I do not know whether it ends the process?

Upvotes: 0

Views: 64

Answers (1)

Mark C.
Mark C.

Reputation: 6460

In the documentation it says:

Each of these methods also requires a callback delegate that is called back with the response of the http call[...] The callback will be called for each response, as and when a response is received and we simply continue on with processing the response as we see fit

Whatever you do with the callbackState delegate, it will hold the response content. That's how you know it will be finished.

Edit: Example from the docs.

      ServicePointManager.DefaultConnectionLimit = 50;
      var url = "http://localhost/HttpTaskServer/default.aspx";

      var iterations = 1000;
      for (int i = 0; i < iterations; i++)
      {
        var postParameters = new NameValueCollection();
        postParameters.Add("data", i.ToString());
        HttpSocket.PostAsync(url, postParameters, callbackState =>
          {
            if (callbackState.Exception != null)
              throw callbackState.Exception;
            Console.WriteLine(HttpSocket.GetResponseText(callbackState.ResponseStream));
          });
      }

Upvotes: 2

Related Questions