1

I have a function like :

public static Task<string> MakeAsyncRequest(string url, string contentType)
        {
            LogMe (TAG, url);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.ContentType = contentType;
            request.Method = WebRequestMethods.Http.Get;
            request.Timeout = 200000;
            request.Proxy = null;


            Task<WebResponse> task = Task.Factory.FromAsync(
                request.BeginGetResponse,
                asyncResult => request.EndGetResponse(asyncResult),
                (object)null);
            return task.ContinueWith(t => ReadStreamFromResponse(t.Result));
        }

So,how to set timeout for this tasK?
For example after 10 sec this task cancelled and toast say "Connection timeout"
I am a beginner in C#
Please assist.

  • [please check this discussion](http://stackoverflow.com/questions/13513650/how-to-set-timeout-for-a-line-of-c-sharp-code) – vITs May 15 '14 at 11:19
  • Also you might want to consider using HttpClient instead with builtin timeout. – SKall May 15 '14 at 12:46

2 Answers2

0

When you use this, the time of the function is 20000ms = 20s for timeout.

This function execute a response for the requested. Use this code to read the response.

private static string ReadStreamFromResponse(WebResponse response)
    {

        using (Stream responseStream = response.GetResponseStream())
        using (StreamReader sr = new StreamReader(responseStream))
        {
            //Need to return this response 
            strContent = sr.ReadToEnd();
        }
        return strContent;

    }

I guess this help you.

I'm working on the same code, please, ask for me if you need.

sincerily Renan costella

-1

CountDownTimer counter = new CountDownTimer(1000 * 10, 0) {

@Override public void onTick(long millisUntilFinished) { }

@Override
public void onFinish() {

          // finish that task

    } 

};

call below methode before before MakeAsyncRequest(...) ;

counter.start();

Nafeez
  • 48
  • 8