0

The following is my background worker thread

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {            
            Thread t1 = new Thread(Thread1);
            t1.Start();
            Thread t2 = new Thread(Thread2);
            t2.Start();
            if (backgroundWorker1.CancellationPending)
            {
                e.Cancel = true;                
            }
        }

Thread1 code is as follows

static void Thread1()
        {
            int nofiles=0;
            int returned = checkforfolderthread(1);
            int startvalue = 0;
            int stopvalue = 5000;
            if (returned == 1)
            {
                nofiles = countfiles();
                startvalue = startvalue + (nofiles - 1) * 1000;
                stopvalue = stopvalue - startvalue;
            }
            repeat(startvalue, stopvalue,1,nofiles-1);

        }

Function called from a thread is as follows

static void repeat(int ini, int fin, int threadno, int startadd)
        {
            int i, j;
            for (j = ini; j < ini + fin; j += 1000)
            {
                StringBuilder sb = new StringBuilder();                
                for (i = j; i < j + 1000; i += 100)
                {
                    WebClient wc = new WebClient();
                    string add = System.String.Format("http://www.colourlovers.com/api/colors/new?numResults=100&resultOffset={0}", i);
                    try
                    {
                        string tobeadded = wc.DownloadString(add);                        
                        sb.AppendLine();
                        sb.Append(tobeadded);
                    }

                    catch (Exception)
                    {
                        break;                        
                    }                    
                }                
                string folderpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                string filename = System.String.Format("DownloadPalette\\Thread{0}\\color{1}.xml",threadno,startadd);

                string location = Path.Combine(folderpath, filename);
                File.WriteAllText(location, sb.ToString());
                startadd = startadd + 1;

            }
        }

What I would want to do is continuously update a progressbar after each for i loop is completed.

But I cannot access the progressbar from this function running in the background thread.

Please Help me

Micha
  • 5,117
  • 8
  • 34
  • 47
user2546342
  • 61
  • 2
  • 6

4 Answers4

0

You miss this method..

// Back on the 'UI' thread so we can update the progress bar
    void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        // The progress percentage is a property of e
        progressBar1.Value = e.ProgressPercentage;
    }

According on this reference : BackgroundWorker and ProgressBar demo

Olrac
  • 1,527
  • 2
  • 10
  • 22
0

You should use invoke, as described here: http://msdn.microsoft.com/en-us/library/ms171728(v=vs.80).aspx

0

First create a method to update the progressbar(create it on the main thread GUI):

private void UpdateBar()
{
    //your code to update
}

Then create a delegate and pass you method to it(this code is also for main thread GUI):

private delegate void UpdateProgressBarDelegate();
private UpdateProgressBarDelegate UpdateProgressBarDelegate_Object;
UpdateProgressBarDelegate_Object = new UpdateProgressBarDelegate(this.UpdateBar);

Now update it from another thread like this:

progressbar.Invoke(UpdateProgressBarDelegate_Object);

Here we are calling the delegate object which will call UpdateBar method on GUI thread with a safe thread call.

Shaharyar
  • 12,254
  • 4
  • 46
  • 66
0

If you need to update more than just the progressbar value you could call a method and checking if an invoke is required. An invoke is required if you want to access a UI object from a separate thread.

private void updateProgress(object sender, int count, int total)
{
    if (base.InvokeRequired)
    {
        base.Invoke(new ProcessCountHandler(this.updateProgress), new object[] { sender, count, total });
    }
    else if (count <= this.progressBar1.Maximum)
    {
        this.progressBar1.Value = count;
        this.CompletedCount.Text = count.ToString("N0") + " of " + total.ToString("N0");
    }
}
Marlon Navas
  • 121
  • 1
  • 3