Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6108577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:18:38+00:00 2026-05-23T14:18:38+00:00

I commonly employ a while loop that continues to try some operation until either

  • 0

I commonly employ a while loop that continues to try some operation until either the operation succeeds or a timeout has elapsed:

bool success = false
int elapsed = 0
while( ( !success ) && ( elapsed < 10000 ) )
{
     Thread.sleep( 1000 );
     elapsed += 1000;
     success = ... some operation ...     
}

I know there a couple of way to implement this, but the basic point is that I repeatedly try some operation with a sleep until success or I’ve slept too long in aggregate.

Is there a built-in .net class/method/etc to save me from re-writing this pattern all over the place? Perhaps input is an Func(of bool) and the timeout?

Edit
Thanks to all who contributed. I opted for the sleep() approach because it was the least complicated and I’m totally anti-complexity =) Here’s my (still needs to be tested) implimentation:

 public static bool RetryUntilSuccessOrTimeout( Func<bool> task , TimeSpan timeout , TimeSpan pause )
    {

        if ( pause.TotalMilliseconds < 0 )
        {
            throw new ArgumentException( "pause must be >= 0 milliseconds" );
        }
        var stopwatch = Stopwatch.StartNew();
        do
        {
            if ( task() ) { return true; }
            Thread.Sleep( ( int )pause.TotalMilliseconds );
        }
        while ( stopwatch.Elapsed < timeout );
        return false;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:18:39+00:00Added an answer on May 23, 2026 at 2:18 pm

    You could wrap your algorithm in a method:

    public bool RetryUntilSuccessOrTimeout(Func<bool> task, TimeSpan timeSpan)
    {
        bool success = false;
        int elapsed = 0;
        while ((!success) && (elapsed < timeSpan.TotalMilliseconds))
        {
            Thread.Sleep(1000);
            elapsed += 1000;
            success = task();
        }
        return success;
    }
    

    and then:

    if (RetryUntilSuccessOrTimeout(() => SomeTask(arg1, arg2), TimeSpan.FromSeconds(10)))
    {
        // the task succeeded
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Some datatypes which I commonly use are string ,integer while creating models *text_field, text_area
It's commonly understood that in the relational model: Every relational operation should yield a
Commonly when I look around the Internet, I find that people are generally using
I commonly find myself extracting common behavior out of classes into helper/utility classes that
To quote Wikipedia : Two commonly used languages that support many kinds of implicit
I know it is commonly used as a lock object, but is that really
It is commonly known that typeof null returns object. However, I have a piece
When building websites I commonly use tools such as Browsershots to ensure that my
Commonly, a website has a menu bar with links to different pages of the
I commonly write code that is along these lines: public class MyClass { static

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.