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

  • Home
  • SEARCH
  • 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 777251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:34:32+00:00 2026-05-14T19:34:32+00:00

I’m working on some code that calls a service. This service call could fail

  • 0

I’m working on some code that calls a service. This service call could fail and if it does I want the system to try again until it works or too much time has passed.

I am wondering where I am going wrong as the following code doesn’t seem to be working correctly… It randomly only does one to four loops…

protected virtual void ProcessAsync(object data, int count)
{
    var worker = new BackgroundWorker();
    worker.DoWork += (sender, e) =>
    {
        throw new InvalidOperationException("oh shiznit!");
    };
    worker.RunWorkerCompleted += (sender, e) =>
    {
        //If an error occurs we need to tell the data about it
        if (e.Error != null)
        {
            count++;
            System.Threading.Thread.Sleep(count * 5000);
            if (count <= 10)
            {
                if (count % 5 == 0)
                    this.Logger.Fatal("LOAD ERROR - The system can't load any data", e.Error);
                else
                    this.Logger.Error("LOAD ERROR - The system can't load any data", e.Error);
                this.ProcessAsync(data, count);
            }
        }
    };
    worker.RunWorkerAsync();
}

Cheers
Anthony

UPDATE:

I’ve switched my code over to use ThreadPool.QueueUserWorkItem instead… Since doing this my problems have gone away and semantically I can do the same thing. Thanks for all your help guys.

  • 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-14T19:34:33+00:00Added an answer on May 14, 2026 at 7:34 pm

    I’ve modified your code slightly and don’t have any issues running through 10 iterations (VS 2008 Express) which leads me to this: Is this the actual code and if not, are you sure you’ve sent enough to reproduce the issue?

    If I were to venture a guess, I would say that the count you’re sending it varies such that count % 5 > 0 and that there’s an exception getting thrown in Logger.Fatal.

    private void button1_Click(object sender, EventArgs e)
    {
        ProcessAsync("beer", 1);
    }
    
    protected virtual void ProcessAsync(object data, int count)
    {
        var worker = new BackgroundWorker();
        worker.DoWork += (sender, e) =>
        {
            throw new InvalidOperationException("oh shiznit!");
        };
        worker.RunWorkerCompleted += (sender, e) =>
        {
            //If an error occurs we need to tell the data about it
            if (e.Error != null)
            {
                count++;
                //System.Threading.Thread.Sleep(count * 5000);
                if (count <= 10)
                {
                    if (count % 5 == 0)
                        this.Logger.Fatal("LOAD ERROR - The system can't load any data - " + count.ToString(), e.Error);
                    else
                        this.Logger.Error("LOAD ERROR - The system can't load any data - " + count.ToString(), e.Error);
                    this.ProcessAsync(data, count);
                }
            }
        };
        worker.RunWorkerAsync();
    }
    
    SomeLogger Logger = new SomeLogger();
    
    class SomeLogger
    {
        public void Fatal(string s, Exception e)
        {
            System.Diagnostics.Debug.WriteLine(s);
        }
    
        public void Error(string s, Exception e)
        {
            System.Diagnostics.Debug.WriteLine(s);
        }
    }
    

    EDIT: A Suggestion
    Put a try-catch around the call to Logger.Fatal and see what happens.

    EDIT: Another suggestion
    I suspect you aren’t sharing enough code for us to help. The key to success here would be to isolate the problem in a dummy project that only has enough code to show the failure. I’d be willing to bet that if you can do that, you most likely wouldn’t need to post this as question here…

    You can start with my assumptions and should see that this works just fine. Then start changing the generalized code into what you’re actually using (I’d start with the real implementation of Logger.Fatal). The error will likely become pretty obvious in short order.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I know there's a lot of other questions out there that deal with this
Does anyone know how can I replace this 2 symbol below from the string
I have some data like this: 1 2 3 4 5 9 2 6

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.