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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:35:47+00:00 2026-06-08T18:35:47+00:00

This code is called in a BackgroundWorker’s DoWork event handler, I am creating a

  • 0

This code is called in a BackgroundWorker’s DoWork event handler, I am creating a WCF client and using it in the try block, if there is an exception I want to abort and retry for 5 times before returning from the handler.

    private void WorkerDoWork(object sender, DoWorkEventArgs e)
    {            
        var mmc = new ServiceClient();

        try
        {
            e.Result = mmc.SubmitData(measure);
        }
        catch (Exception)
        {
            mmc.Abort();

            mmc = new ServiceClient();

            var counter = 0;

            while ((bool)e.Result == false && counter++ < 5)
            {
                try
                {
                    e.Result = mmc.SubmitData(measure);
                }
                catch (Exception)
                {
                    mmc.Abort();
                    mmc = new ServiceClient();
                }
            }
        }
        finally
        {
            if (mmc.State == CommunicationState.Faulted)
            {
                mmc.Abort();
            }
            else
            {
                mmc.Close();
            }
        }
     }

I am not realy happy with the code, feels like something is fishy with it! Specially the nested try/catch.

Is this code ok? or should I refactor it?

  • 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-08T18:35:49+00:00Added an answer on June 8, 2026 at 6:35 pm

    Yes, I agree the hammering of the web service blindly is the problem.

    It is good to know what possible exceptions you can handle.

    This looks like you are only dealing with an unreachable endpoint when you could instead be getting a warning about data format, types, limits, restrictions, authentication failure, account usage limit hits, ANYTHING!

    But eveything is treated like a retry would fix it, and if it doesn’t, it’s broke!

    How about some reporting on the problems encountered?

    As a good programmer, you should find out what exceptions will be thrown for what situations and handle them, if you can, in an appropriate manner.
    If you cant handle them, then report them to logging or to the user (if it is interactive).

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

Sidebar

Related Questions

I have this code which is called at an onChange event of an function
AFAIK, this event is promised to be called in the creator thread ( BackgroundWorker
This is code called by main to sort an array using selections sort in
So I have this code which is called in a print_info() function after a
This code attempts to resize images in a directory called imgs. Unfortunately for some
I have this column called code that look like : 1-100-ETC-60 , 1-100-ETC-20 ,
In this code fragment, which constructor is actually called? Vector v = getVector(); Vector
I currently have this code which stores XML into an XML-type column called data,
When i run this code the paintComponent method is not being called It may
in this code, if collection is released in the class that called init... ,

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.