How can I await a minimum amount of time?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
6
down vote

favorite












I have an async C# method where I am getting an HTTP resource, and I am doing it in an infinite loop. However I don't want to hit the resource too quickly. My current code is:



HttpClient http = new HttpClient();
while (true)
// Long-poll the API
var response = await http.GetAsync(buildUri());

Console.WriteLine("Resp: " + response.ToString());
Console.WriteLine("CONTENT:");
Console.WriteLine(await response.Content.ReadAsStringAsync());



However, I want to be able to make sure I don't make an HTTP request more often than once per 10 seconds. So I want to start a 10 second timer at the beginning of the loop, and at the end say "await the completion of the 10 second timer". Is there a way to do this in C#.NET?










share|improve this question

















  • 1




    Yes. Suggest the Polly Framework
    – Crowcoder
    6 hours ago














up vote
6
down vote

favorite












I have an async C# method where I am getting an HTTP resource, and I am doing it in an infinite loop. However I don't want to hit the resource too quickly. My current code is:



HttpClient http = new HttpClient();
while (true)
// Long-poll the API
var response = await http.GetAsync(buildUri());

Console.WriteLine("Resp: " + response.ToString());
Console.WriteLine("CONTENT:");
Console.WriteLine(await response.Content.ReadAsStringAsync());



However, I want to be able to make sure I don't make an HTTP request more often than once per 10 seconds. So I want to start a 10 second timer at the beginning of the loop, and at the end say "await the completion of the 10 second timer". Is there a way to do this in C#.NET?










share|improve this question

















  • 1




    Yes. Suggest the Polly Framework
    – Crowcoder
    6 hours ago












up vote
6
down vote

favorite









up vote
6
down vote

favorite











I have an async C# method where I am getting an HTTP resource, and I am doing it in an infinite loop. However I don't want to hit the resource too quickly. My current code is:



HttpClient http = new HttpClient();
while (true)
// Long-poll the API
var response = await http.GetAsync(buildUri());

Console.WriteLine("Resp: " + response.ToString());
Console.WriteLine("CONTENT:");
Console.WriteLine(await response.Content.ReadAsStringAsync());



However, I want to be able to make sure I don't make an HTTP request more often than once per 10 seconds. So I want to start a 10 second timer at the beginning of the loop, and at the end say "await the completion of the 10 second timer". Is there a way to do this in C#.NET?










share|improve this question













I have an async C# method where I am getting an HTTP resource, and I am doing it in an infinite loop. However I don't want to hit the resource too quickly. My current code is:



HttpClient http = new HttpClient();
while (true)
// Long-poll the API
var response = await http.GetAsync(buildUri());

Console.WriteLine("Resp: " + response.ToString());
Console.WriteLine("CONTENT:");
Console.WriteLine(await response.Content.ReadAsStringAsync());



However, I want to be able to make sure I don't make an HTTP request more often than once per 10 seconds. So I want to start a 10 second timer at the beginning of the loop, and at the end say "await the completion of the 10 second timer". Is there a way to do this in C#.NET?







c# .net asynchronous






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 6 hours ago









Jez

11.2k1878144




11.2k1878144







  • 1




    Yes. Suggest the Polly Framework
    – Crowcoder
    6 hours ago












  • 1




    Yes. Suggest the Polly Framework
    – Crowcoder
    6 hours ago







1




1




Yes. Suggest the Polly Framework
– Crowcoder
6 hours ago




Yes. Suggest the Polly Framework
– Crowcoder
6 hours ago












5 Answers
5






active

oldest

votes

















up vote
4
down vote



accepted










At the simplest:



while (true) 
var rateLimit = Task.Delay(TimeSpan.FromSeconds(10));
// ...await your http/whatever
await rateLimit;



The await rateLimit will complete immediately if the http work took over the 10 seconds.



However, you may choose to compare the times before and after the http work, to see if you even need to wait; more like:



while (true) 
var timeBefore = ...
// ...await your http/whatever
var timeAfter = ...
// ... calculate time left
if (timeLeft > TimeSpan.Zero) await Task.Delay(timeLeft);



This avoids the plumbing needed for Task.Delay in the scenarios where it would complete immediately.






share|improve this answer




















  • Does that Task.Delay plumbing really add much overhead?
    – Jez
    6 hours ago






  • 1




    @Jez context is everything; I don't know your scenario! if you have large concurrency, I would try to avoid it - it hooks into Timer; nothing too bad, but worth avoiding if not needed in some cases
    – Marc Gravell♦
    6 hours ago

















up vote
4
down vote













Create a StopWatch (and start it) when you start the loop.



Then when GetAsync method returns, you read the ElapsedMilliseconds. Subtract this value from 10000. Now do:



await Task.Delay(yourValue);


Now it takes min. 10 seconds for a loop.



The code:



while (true) 
// Long-poll the API

StopWatch sw = StopWatch.StartNew();
var response = await http.GetAsync(buildUri());

Console.WriteLine("Resp: " + response.ToString());
Console.WriteLine("CONTENT:");
Console.WriteLine(await response.Content.ReadAsStringAsync());

int milliseconds = 10000 - sw.ElapsedMilliSeconds;
if (milliseconds > 0)

await Task.Delay(milliseconds);







share|improve this answer





























    up vote
    3
    down vote













    You can use a simple StopWatch for this, and use Task.Delay to wait the required time, if any.



    const int minimumTime = 10_000;
    var sw = StopWatch.StartNew();

    while (true)

    // Long-poll the API
    var response = await http.GetAsync(buildUri());
    ...
    int difference = minimumTime - sw.EllapsedMiliseconds;

    if (difference > 0)

    await Task.Delay(difference);


    sw.Restart();



    This, however, is only for the case that you want 10 seconds between each call. If the API takes 9 seconds to complete, you'd get 2 calls in a second.

    If you want to always wait 10 seconds, regardless of how much it took the API to complete, just use:



    const int waitTime = 10_000;

    while (true)

    // Long-poll the API
    var response = await http.GetAsync(buildUri());
    ...

    await Task.Delay(waitTime);






    share|improve this answer





























      up vote
      1
      down vote













      Not sure if that's exactly what you are looking for, but Task.Delay can suspend the loop for the given milliseconds.



      while (true)

      ...
      await Task.Delay(10 * 1000);






      share|improve this answer



























        up vote
        1
        down vote













        You could use CancellationTokenSource combined with TaskCompletionSource to create a "delay" task that starts when your request starts, and which you await when your request is complete:



        private static Task GetDelayTask(TimeSpan delay, object state = null)

        var taskSource = new TaskCompletionSource<object>();
        var cts = new CancellationTokenSource(delay);
        cts.Token.Register(() =>

        taskSource.SetResult(state);
        cts.Dispose();
        );
        return taskSource.Task;



        Usage:



        HttpClient http = new HttpClient();
        while (true)

        var delayTask = GetDelayTask(TimeSpan.FromSeconds(10));
        // Long-poll the API
        var response = await http.GetAsync(buildUri());

        Console.WriteLine("Resp: " + response.ToString());
        Console.WriteLine("CONTENT:");
        Console.WriteLine(await response.Content.ReadAsStringAsync());
        await delayTask;






        share|improve this answer




















          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53181980%2fhow-can-i-await-a-minimum-amount-of-time%23new-answer', 'question_page');

          );

          Post as a guest






























          5 Answers
          5






          active

          oldest

          votes








          5 Answers
          5






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          4
          down vote



          accepted










          At the simplest:



          while (true) 
          var rateLimit = Task.Delay(TimeSpan.FromSeconds(10));
          // ...await your http/whatever
          await rateLimit;



          The await rateLimit will complete immediately if the http work took over the 10 seconds.



          However, you may choose to compare the times before and after the http work, to see if you even need to wait; more like:



          while (true) 
          var timeBefore = ...
          // ...await your http/whatever
          var timeAfter = ...
          // ... calculate time left
          if (timeLeft > TimeSpan.Zero) await Task.Delay(timeLeft);



          This avoids the plumbing needed for Task.Delay in the scenarios where it would complete immediately.






          share|improve this answer




















          • Does that Task.Delay plumbing really add much overhead?
            – Jez
            6 hours ago






          • 1




            @Jez context is everything; I don't know your scenario! if you have large concurrency, I would try to avoid it - it hooks into Timer; nothing too bad, but worth avoiding if not needed in some cases
            – Marc Gravell♦
            6 hours ago














          up vote
          4
          down vote



          accepted










          At the simplest:



          while (true) 
          var rateLimit = Task.Delay(TimeSpan.FromSeconds(10));
          // ...await your http/whatever
          await rateLimit;



          The await rateLimit will complete immediately if the http work took over the 10 seconds.



          However, you may choose to compare the times before and after the http work, to see if you even need to wait; more like:



          while (true) 
          var timeBefore = ...
          // ...await your http/whatever
          var timeAfter = ...
          // ... calculate time left
          if (timeLeft > TimeSpan.Zero) await Task.Delay(timeLeft);



          This avoids the plumbing needed for Task.Delay in the scenarios where it would complete immediately.






          share|improve this answer




















          • Does that Task.Delay plumbing really add much overhead?
            – Jez
            6 hours ago






          • 1




            @Jez context is everything; I don't know your scenario! if you have large concurrency, I would try to avoid it - it hooks into Timer; nothing too bad, but worth avoiding if not needed in some cases
            – Marc Gravell♦
            6 hours ago












          up vote
          4
          down vote



          accepted







          up vote
          4
          down vote



          accepted






          At the simplest:



          while (true) 
          var rateLimit = Task.Delay(TimeSpan.FromSeconds(10));
          // ...await your http/whatever
          await rateLimit;



          The await rateLimit will complete immediately if the http work took over the 10 seconds.



          However, you may choose to compare the times before and after the http work, to see if you even need to wait; more like:



          while (true) 
          var timeBefore = ...
          // ...await your http/whatever
          var timeAfter = ...
          // ... calculate time left
          if (timeLeft > TimeSpan.Zero) await Task.Delay(timeLeft);



          This avoids the plumbing needed for Task.Delay in the scenarios where it would complete immediately.






          share|improve this answer












          At the simplest:



          while (true) 
          var rateLimit = Task.Delay(TimeSpan.FromSeconds(10));
          // ...await your http/whatever
          await rateLimit;



          The await rateLimit will complete immediately if the http work took over the 10 seconds.



          However, you may choose to compare the times before and after the http work, to see if you even need to wait; more like:



          while (true) 
          var timeBefore = ...
          // ...await your http/whatever
          var timeAfter = ...
          // ... calculate time left
          if (timeLeft > TimeSpan.Zero) await Task.Delay(timeLeft);



          This avoids the plumbing needed for Task.Delay in the scenarios where it would complete immediately.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 6 hours ago









          Marc Gravell♦

          767k19021112523




          767k19021112523











          • Does that Task.Delay plumbing really add much overhead?
            – Jez
            6 hours ago






          • 1




            @Jez context is everything; I don't know your scenario! if you have large concurrency, I would try to avoid it - it hooks into Timer; nothing too bad, but worth avoiding if not needed in some cases
            – Marc Gravell♦
            6 hours ago
















          • Does that Task.Delay plumbing really add much overhead?
            – Jez
            6 hours ago






          • 1




            @Jez context is everything; I don't know your scenario! if you have large concurrency, I would try to avoid it - it hooks into Timer; nothing too bad, but worth avoiding if not needed in some cases
            – Marc Gravell♦
            6 hours ago















          Does that Task.Delay plumbing really add much overhead?
          – Jez
          6 hours ago




          Does that Task.Delay plumbing really add much overhead?
          – Jez
          6 hours ago




          1




          1




          @Jez context is everything; I don't know your scenario! if you have large concurrency, I would try to avoid it - it hooks into Timer; nothing too bad, but worth avoiding if not needed in some cases
          – Marc Gravell♦
          6 hours ago




          @Jez context is everything; I don't know your scenario! if you have large concurrency, I would try to avoid it - it hooks into Timer; nothing too bad, but worth avoiding if not needed in some cases
          – Marc Gravell♦
          6 hours ago












          up vote
          4
          down vote













          Create a StopWatch (and start it) when you start the loop.



          Then when GetAsync method returns, you read the ElapsedMilliseconds. Subtract this value from 10000. Now do:



          await Task.Delay(yourValue);


          Now it takes min. 10 seconds for a loop.



          The code:



          while (true) 
          // Long-poll the API

          StopWatch sw = StopWatch.StartNew();
          var response = await http.GetAsync(buildUri());

          Console.WriteLine("Resp: " + response.ToString());
          Console.WriteLine("CONTENT:");
          Console.WriteLine(await response.Content.ReadAsStringAsync());

          int milliseconds = 10000 - sw.ElapsedMilliSeconds;
          if (milliseconds > 0)

          await Task.Delay(milliseconds);







          share|improve this answer


























            up vote
            4
            down vote













            Create a StopWatch (and start it) when you start the loop.



            Then when GetAsync method returns, you read the ElapsedMilliseconds. Subtract this value from 10000. Now do:



            await Task.Delay(yourValue);


            Now it takes min. 10 seconds for a loop.



            The code:



            while (true) 
            // Long-poll the API

            StopWatch sw = StopWatch.StartNew();
            var response = await http.GetAsync(buildUri());

            Console.WriteLine("Resp: " + response.ToString());
            Console.WriteLine("CONTENT:");
            Console.WriteLine(await response.Content.ReadAsStringAsync());

            int milliseconds = 10000 - sw.ElapsedMilliSeconds;
            if (milliseconds > 0)

            await Task.Delay(milliseconds);







            share|improve this answer
























              up vote
              4
              down vote










              up vote
              4
              down vote









              Create a StopWatch (and start it) when you start the loop.



              Then when GetAsync method returns, you read the ElapsedMilliseconds. Subtract this value from 10000. Now do:



              await Task.Delay(yourValue);


              Now it takes min. 10 seconds for a loop.



              The code:



              while (true) 
              // Long-poll the API

              StopWatch sw = StopWatch.StartNew();
              var response = await http.GetAsync(buildUri());

              Console.WriteLine("Resp: " + response.ToString());
              Console.WriteLine("CONTENT:");
              Console.WriteLine(await response.Content.ReadAsStringAsync());

              int milliseconds = 10000 - sw.ElapsedMilliSeconds;
              if (milliseconds > 0)

              await Task.Delay(milliseconds);







              share|improve this answer














              Create a StopWatch (and start it) when you start the loop.



              Then when GetAsync method returns, you read the ElapsedMilliseconds. Subtract this value from 10000. Now do:



              await Task.Delay(yourValue);


              Now it takes min. 10 seconds for a loop.



              The code:



              while (true) 
              // Long-poll the API

              StopWatch sw = StopWatch.StartNew();
              var response = await http.GetAsync(buildUri());

              Console.WriteLine("Resp: " + response.ToString());
              Console.WriteLine("CONTENT:");
              Console.WriteLine(await response.Content.ReadAsStringAsync());

              int milliseconds = 10000 - sw.ElapsedMilliSeconds;
              if (milliseconds > 0)

              await Task.Delay(milliseconds);








              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited 5 hours ago

























              answered 6 hours ago









              Poul Bak

              4,4692631




              4,4692631




















                  up vote
                  3
                  down vote













                  You can use a simple StopWatch for this, and use Task.Delay to wait the required time, if any.



                  const int minimumTime = 10_000;
                  var sw = StopWatch.StartNew();

                  while (true)

                  // Long-poll the API
                  var response = await http.GetAsync(buildUri());
                  ...
                  int difference = minimumTime - sw.EllapsedMiliseconds;

                  if (difference > 0)

                  await Task.Delay(difference);


                  sw.Restart();



                  This, however, is only for the case that you want 10 seconds between each call. If the API takes 9 seconds to complete, you'd get 2 calls in a second.

                  If you want to always wait 10 seconds, regardless of how much it took the API to complete, just use:



                  const int waitTime = 10_000;

                  while (true)

                  // Long-poll the API
                  var response = await http.GetAsync(buildUri());
                  ...

                  await Task.Delay(waitTime);






                  share|improve this answer


























                    up vote
                    3
                    down vote













                    You can use a simple StopWatch for this, and use Task.Delay to wait the required time, if any.



                    const int minimumTime = 10_000;
                    var sw = StopWatch.StartNew();

                    while (true)

                    // Long-poll the API
                    var response = await http.GetAsync(buildUri());
                    ...
                    int difference = minimumTime - sw.EllapsedMiliseconds;

                    if (difference > 0)

                    await Task.Delay(difference);


                    sw.Restart();



                    This, however, is only for the case that you want 10 seconds between each call. If the API takes 9 seconds to complete, you'd get 2 calls in a second.

                    If you want to always wait 10 seconds, regardless of how much it took the API to complete, just use:



                    const int waitTime = 10_000;

                    while (true)

                    // Long-poll the API
                    var response = await http.GetAsync(buildUri());
                    ...

                    await Task.Delay(waitTime);






                    share|improve this answer
























                      up vote
                      3
                      down vote










                      up vote
                      3
                      down vote









                      You can use a simple StopWatch for this, and use Task.Delay to wait the required time, if any.



                      const int minimumTime = 10_000;
                      var sw = StopWatch.StartNew();

                      while (true)

                      // Long-poll the API
                      var response = await http.GetAsync(buildUri());
                      ...
                      int difference = minimumTime - sw.EllapsedMiliseconds;

                      if (difference > 0)

                      await Task.Delay(difference);


                      sw.Restart();



                      This, however, is only for the case that you want 10 seconds between each call. If the API takes 9 seconds to complete, you'd get 2 calls in a second.

                      If you want to always wait 10 seconds, regardless of how much it took the API to complete, just use:



                      const int waitTime = 10_000;

                      while (true)

                      // Long-poll the API
                      var response = await http.GetAsync(buildUri());
                      ...

                      await Task.Delay(waitTime);






                      share|improve this answer














                      You can use a simple StopWatch for this, and use Task.Delay to wait the required time, if any.



                      const int minimumTime = 10_000;
                      var sw = StopWatch.StartNew();

                      while (true)

                      // Long-poll the API
                      var response = await http.GetAsync(buildUri());
                      ...
                      int difference = minimumTime - sw.EllapsedMiliseconds;

                      if (difference > 0)

                      await Task.Delay(difference);


                      sw.Restart();



                      This, however, is only for the case that you want 10 seconds between each call. If the API takes 9 seconds to complete, you'd get 2 calls in a second.

                      If you want to always wait 10 seconds, regardless of how much it took the API to complete, just use:



                      const int waitTime = 10_000;

                      while (true)

                      // Long-poll the API
                      var response = await http.GetAsync(buildUri());
                      ...

                      await Task.Delay(waitTime);







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited 6 hours ago

























                      answered 6 hours ago









                      Camilo Terevinto

                      17k63261




                      17k63261




















                          up vote
                          1
                          down vote













                          Not sure if that's exactly what you are looking for, but Task.Delay can suspend the loop for the given milliseconds.



                          while (true)

                          ...
                          await Task.Delay(10 * 1000);






                          share|improve this answer
























                            up vote
                            1
                            down vote













                            Not sure if that's exactly what you are looking for, but Task.Delay can suspend the loop for the given milliseconds.



                            while (true)

                            ...
                            await Task.Delay(10 * 1000);






                            share|improve this answer






















                              up vote
                              1
                              down vote










                              up vote
                              1
                              down vote









                              Not sure if that's exactly what you are looking for, but Task.Delay can suspend the loop for the given milliseconds.



                              while (true)

                              ...
                              await Task.Delay(10 * 1000);






                              share|improve this answer












                              Not sure if that's exactly what you are looking for, but Task.Delay can suspend the loop for the given milliseconds.



                              while (true)

                              ...
                              await Task.Delay(10 * 1000);







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered 6 hours ago









                              MichaC

                              10.8k23148




                              10.8k23148




















                                  up vote
                                  1
                                  down vote













                                  You could use CancellationTokenSource combined with TaskCompletionSource to create a "delay" task that starts when your request starts, and which you await when your request is complete:



                                  private static Task GetDelayTask(TimeSpan delay, object state = null)

                                  var taskSource = new TaskCompletionSource<object>();
                                  var cts = new CancellationTokenSource(delay);
                                  cts.Token.Register(() =>

                                  taskSource.SetResult(state);
                                  cts.Dispose();
                                  );
                                  return taskSource.Task;



                                  Usage:



                                  HttpClient http = new HttpClient();
                                  while (true)

                                  var delayTask = GetDelayTask(TimeSpan.FromSeconds(10));
                                  // Long-poll the API
                                  var response = await http.GetAsync(buildUri());

                                  Console.WriteLine("Resp: " + response.ToString());
                                  Console.WriteLine("CONTENT:");
                                  Console.WriteLine(await response.Content.ReadAsStringAsync());
                                  await delayTask;






                                  share|improve this answer
























                                    up vote
                                    1
                                    down vote













                                    You could use CancellationTokenSource combined with TaskCompletionSource to create a "delay" task that starts when your request starts, and which you await when your request is complete:



                                    private static Task GetDelayTask(TimeSpan delay, object state = null)

                                    var taskSource = new TaskCompletionSource<object>();
                                    var cts = new CancellationTokenSource(delay);
                                    cts.Token.Register(() =>

                                    taskSource.SetResult(state);
                                    cts.Dispose();
                                    );
                                    return taskSource.Task;



                                    Usage:



                                    HttpClient http = new HttpClient();
                                    while (true)

                                    var delayTask = GetDelayTask(TimeSpan.FromSeconds(10));
                                    // Long-poll the API
                                    var response = await http.GetAsync(buildUri());

                                    Console.WriteLine("Resp: " + response.ToString());
                                    Console.WriteLine("CONTENT:");
                                    Console.WriteLine(await response.Content.ReadAsStringAsync());
                                    await delayTask;






                                    share|improve this answer






















                                      up vote
                                      1
                                      down vote










                                      up vote
                                      1
                                      down vote









                                      You could use CancellationTokenSource combined with TaskCompletionSource to create a "delay" task that starts when your request starts, and which you await when your request is complete:



                                      private static Task GetDelayTask(TimeSpan delay, object state = null)

                                      var taskSource = new TaskCompletionSource<object>();
                                      var cts = new CancellationTokenSource(delay);
                                      cts.Token.Register(() =>

                                      taskSource.SetResult(state);
                                      cts.Dispose();
                                      );
                                      return taskSource.Task;



                                      Usage:



                                      HttpClient http = new HttpClient();
                                      while (true)

                                      var delayTask = GetDelayTask(TimeSpan.FromSeconds(10));
                                      // Long-poll the API
                                      var response = await http.GetAsync(buildUri());

                                      Console.WriteLine("Resp: " + response.ToString());
                                      Console.WriteLine("CONTENT:");
                                      Console.WriteLine(await response.Content.ReadAsStringAsync());
                                      await delayTask;






                                      share|improve this answer












                                      You could use CancellationTokenSource combined with TaskCompletionSource to create a "delay" task that starts when your request starts, and which you await when your request is complete:



                                      private static Task GetDelayTask(TimeSpan delay, object state = null)

                                      var taskSource = new TaskCompletionSource<object>();
                                      var cts = new CancellationTokenSource(delay);
                                      cts.Token.Register(() =>

                                      taskSource.SetResult(state);
                                      cts.Dispose();
                                      );
                                      return taskSource.Task;



                                      Usage:



                                      HttpClient http = new HttpClient();
                                      while (true)

                                      var delayTask = GetDelayTask(TimeSpan.FromSeconds(10));
                                      // Long-poll the API
                                      var response = await http.GetAsync(buildUri());

                                      Console.WriteLine("Resp: " + response.ToString());
                                      Console.WriteLine("CONTENT:");
                                      Console.WriteLine(await response.Content.ReadAsStringAsync());
                                      await delayTask;







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered 6 hours ago









                                      John

                                      10k31734




                                      10k31734



























                                           

                                          draft saved


                                          draft discarded















































                                           


                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function ()
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53181980%2fhow-can-i-await-a-minimum-amount-of-time%23new-answer', 'question_page');

                                          );

                                          Post as a guest













































































                                          Comments

                                          Popular posts from this blog

                                          Long meetings (6-7 hours a day): Being “babysat” by supervisor

                                          Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                                          Confectionery