C#列表框连续循环

本文关键字:循环 连续 列表 | 更新日期: 2023-09-27 18:01:02

我能让这个循环继续下去吗?在列表框中的最后一个项目之后,转到第一个项目,以此类推…

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);
            }
        }
    }
}

如有任何帮助,我们将不胜感激!

感谢您提供的所有答案

我的名单好像倒退了,所以我必须更换

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

带有

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

C#列表框连续循环

你正在倒计时,所以我认为是"在第一个之后,(再次(转到最后一个"。

你的for循环可以变成:

int i = 0;
for(;;)  // or while(true)
{
    if (i <= 0)
      i = listBox1.Items.Count;
    i -= 1;
    if (worker.CancellationPending)
    {
        ...
    }
}

但我注意到您正在从bgw中的ListBox中读取,这不是线程安全的。即使它看起来可以工作,当ListBox更改时,您也可能得到null值或异常。很少。

编辑

走另一条路更容易:

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

类似的东西?

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);
            }
        }
    }
}
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;
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;
    }