BackgroundWorker

Posted by vdh_ant on Stack Overflow See other posts from Stack Overflow or by vdh_ant
Published on 2010-05-11T04:21:36Z Indexed on 2010/05/11 4:24 UTC
Read the original article Hit count: 225

Hi guys

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

© Stack Overflow or respective owner

Related posts about .NET

Related posts about multithreading