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 8134029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:52:08+00:00 2026-06-06T09:52:08+00:00

I constantly have some situations in which I have to retry some operations if

  • 0

I constantly have some situations in which I have to retry some operations if they fail, giving up after certain number of times, and taking a short breaks between attempts.

Is there a way to create a ‘retry method’ that will enable me not to copy the code around every time I do that?

  • 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-06-06T09:52:09+00:00Added an answer on June 6, 2026 at 9:52 am

    Got tired of copy/paste the same code over and over again, so I created a method that accepts the delegate of the task that has to be done. Here it is:

    //  logger declaration (I use NLog)
    private static readonly Logger Log = LogManager.GetCurrentClassLogger();
    delegate void WhatTodo();
    static void TrySeveralTimes(WhatTodo Task, int Retries, int RetryDelay)
    {
        int retries = 0;
        while (true)
        {
            try
            {
                Task();
                break;
            }
            catch (Exception ex)
            {
                retries++;
                Log.Info<string, int>("Problem doing it {0}, try {1}", ex.Message, retries);
                if (retries > Retries)
                {
                    Log.Info("Giving up...");
                    throw;
                }
                Thread.Sleep(RetryDelay);
            }
        }
    }
    

    To use it, I would simply write:

    TrySeveralTimes(() =>
    {
        string destinationVpr = Path.Combine(outdir, "durations.vpr");
        File.AppendAllText(destinationVpr, file + ",     " + lengthInMiliseconds.ToString() + "\r\n");
    }, 10, 100);
    

    In this example I am appending a file that gets locked with some external process, only way to write it is to retry it several times until the process is done with it…

    I would sure love to see better ways of handling this particular pattern (retrying).

    EDIT: I looked at Gallio in another answer, and it is really great. Look at this example:

    Retry.Repeat(10) // Retries maximum 10 times the evaluation of the condition.
             .WithPolling(TimeSpan.FromSeconds(1)) // Waits approximatively for 1 second between each evaluation of the condition.
             .WithTimeout(TimeSpan.FromSeconds(30)) // Sets a timeout of 30 seconds.
             .DoBetween(() => { /* DoSomethingBetweenEachCall */ })
             .Until(() => { return EvaluateSomeCondition(); });
    

    It does everything. It will even watch your kids while you code 🙂 But, I strive for simplicity, and still am using .NET 2.0. So I guess that my example will still be of some use to you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Model which has some constants defined, like below: class Order(models.Model): WAITING
I have a C++ class implementation file which has some constants. The constants appear
I have a property in my view model which returns a constant under some
I have a vector that is constantly being updated (about 5-20 times a second)
Following situation: I have an array with some constant values, which represent ranges. A
Hey I have the following situation: a WinService which is processing some stuff from
I constantly have to update main images on my site, the user will go
Should I constantly have a session open in my web site with user accounts,
I'm editing a CSS style sheet in Eclipse and I constantly have to copy
I have an application that needs updating constantly. I would like to create a

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.