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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:33:18+00:00 2026-05-15T15:33:18+00:00

I am using an external automation library with bunch of APIs with either 1

  • 0

I am using an external automation library with bunch of APIs with either 1 or 2 parameters which randomly throws TargetInvocationException. Calling these APIs second or third time usually works. I therefore created two helper methods to encapsulate the multiple retry logic

//Original API calls
bool result1 = Foo1(true);
int result2 = Foo2(4, "abc");

//New API calls
bool result1 = SafeMethodCall(Foo1, true);
int result2 = SafeMethodCall(Foo2, 4, "abc");


//Helper Methods
public static TResult SafeMethodCall<T, TResult>(
    Func<T, TResult> unSafeMethod,
    T parameter)
{
    int numberOfMethodInvocationAttempts = 3;
    int sleepIntervalBetweenMethodInvocations = 10000;

    for (int i = 0; i < numberOfMethodInvocationAttempts; i++)
    {
        try
        {
            return unSafeMethod(parameter);
        }
        catch (System.Reflection.TargetInvocationException ex)
        {
            System.Threading.Thread.Sleep(sleepIntervalBetweenMethodInvocations);
        }
    }
}

public static TResult SafeTargetInvocationMethodCall<T1, T2, TResult>(
    Func<T1, T2, TResult> unSafeMethod,
    T1 parameter1,
    T2 parameter2)
{
    int numberOfMethodInvocationAttempts = 3;
    int sleepIntervalBetweenMethodInvocations = 10000;

    for (int i = 0; i < numberOfMethodInvocationAttempts; i++)
    {
        try
        {
            return unSafeMethod(parameter1, parameter2);
        }
        catch (System.Reflection.TargetInvocationException ex)
        {
            System.Threading.Thread.Sleep(sleepIntervalBetweenMethodInvocations);
        }
    }
}

Problem: If you see the two helper methods above have the same body and the only difference is unsafeMethod call inside the try block. How can I avoid code duplication here as I might have to add a overloaded method that accepts

Func<TResult>

as another parameter type.

  • 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-15T15:33:19+00:00Added an answer on May 15, 2026 at 3:33 pm

    Just pass in Func<TResult> and call it like this:

    bool result1 = SafeMethodCall(() => Foo1(true));
    int result2 = SafeMethodCall(() => Foo2(4, "abc"));
    

    In other words, encapsulate the arguments in the delegate itself.

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

Sidebar

Related Questions

I am using the external library IMAPClient. When the login fails, i see this
This is my first time using an external library, and I'm a bit nervous
In my application I'm using an external library (Batik 1.7) that is made up
I'm using external library, jquery.cookies.2.2.0.min.js , and according to the documentation you get a
I am using external library ( .dll ), some of it's methods (including constructors)
One of the side-effects of using an external Subversion repository was getting automatic offsite
I am trying to clear images using external jquery file but it's not showing
I'm using an external API to receive xml and serializing this to an object
I'm using an external swf containing a number of font classes to load and
Instead of using an external web-based Mercurial host, I want to set one up

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.