Lindys
Lindys

Reputation: 3

C# listbox continuous loop

can i make this loop continue in such a way that after last item in listbox to go to the first one and so on...

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    BackgroundWorker worker = sender as BackgroundWorker;
    for (int i = listBox1.Items.Count - 1; i >= 0; i--)
    {
        {
            if (worker.CancellationPending == true)
            {
                e.Cancel = true;
                break;
            }
            else
            {
                string queryhere = listBox1.Items[i].ToString();
                this.SetTextappend("" + queryhere + "\n");
                System.Threading.Thread.Sleep(500);
                worker.ReportProgress(i * 1);
            }
        }
    }
}

Any help would be greatly appreciated!

Thank you for all the answers

it seems that my list was going backwads so i must replace

 for (int i = listBox1.Items.Count - 1; i >= 0; i--)

with

 for (int i=0;i<ListBox1.Items.Count;i++)

Upvotes: 0

Views: 1494

Answers (4)

Henk Holterman
Henk Holterman

Reputation: 273464

You are counting down so I think it is "after the first one, go to the last (again)".

Your for-loop can become:

int i = 0;
for(;;)  // or while(true)
{
    if (i <= 0)
      i = listBox1.Items.Count;

    i -= 1;

    if (worker.CancellationPending)
    {
        ...
    }

}

But I notice you are reading from a ListBox inside a bgw, that is not thread-safe. Even if it may appear to work, when the ListBox changes you could get null values or exceptions. Very infrequently.

Edit

And going the other way is even easier:

int i = -1;
for(;;)  // or while(true)
{
    i = (i + 1) % listBox1.Items.Count;

    if (worker.CancellationPending)
    {
        ...
    }  
}

Upvotes: 2

Bolu
Bolu

Reputation: 8786

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {    
        BackgroundWorker worker = sender as BackgroundWorker;
        while (worker.CancellationPending != true)
        {
            for (int i = listBox1.Items.Count - 1; i >= 0; i--)
            {
                   if(worker.CancellationPending != true)
                   {
                      string queryhere = listBox1.Items[i].ToString();
                      this.SetTextappend("" + queryhere + "\n");
                      System.Threading.Thread.Sleep(500);
                      worker.ReportProgress(i * 1);
                   }
                   else
                   {
                      break;
                   } 
            }
        }
        e.Cancel = true;
    }

Upvotes: 0

Zruty
Zruty

Reputation: 8667

int N = listbox1.Items.Count;
for (int i=N-1; !worker.CancellationPending; i = (i+N-1) % N ) 
{
   // this weird calculation i=(i+N-1)%N is because I'm not sure whether C# 
   // divides negative integers properly (so that the remainder is non-negative)
   // It could be i=(i-1)%N . 
   string queryhere = listBox1.Items[i].ToString();
   this.SetTextappend("" + queryhere + "\n");
   System.Threading.Thread.Sleep(500);
   worker.ReportProgress(i * 1); // by the way, there should be a percentage.
                                 // You better revise the progress reporting.
}
e.Cancel = true;

Upvotes: 0

digEmAll
digEmAll

Reputation: 57220

Something like this ?

BackgroundWorker worker = sender as BackgroundWorker;
bool go = true;
while(go)
{
    for (int i = listBox1.Items.Count - 1; i >= 0; i--)
    {
        {
            if (worker.CancellationPending == true)
            {
                e.Cancel = true;
                go = false;
                break;
            }
            else
            {
                string queryhere = listBox1.Items[i].ToString();
                this.SetTextappend("" + queryhere + "\n");
                System.Threading.Thread.Sleep(500);
                worker.ReportProgress(i * 1);
            }
        }
    }
}

Upvotes: 1

Related Questions