0

我可以让这个循环以这样的方式继续,在列表框中的最后一个项目之后转到第一个等等......

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++)
4

4 回答 4

2

你在倒计时,所以我认为它是“在第一个之后,去最后一个(再次)”。

你的 for 循环可以变成:

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

    i -= 1;

    if (worker.CancellationPending)
    {
        ...
    }

}

但我注意到您正在从 bgw 内的 ListBox 中读取,这不是线程安全的。即使它看起来有效,但当 ListBox 更改时,您可能会得到空值或异常。非常罕见。

编辑

走另一条路更容易:

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

    if (worker.CancellationPending)
    {
        ...
    }  
}
于 2011-07-05T09:58:41.137 回答
1

像这样的东西?

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);
            }
        }
    }
}
于 2011-07-05T09:59:28.897 回答
0
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;
于 2011-07-05T10:00:08.380 回答
0
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;
    }
于 2011-07-05T10:03:46.730 回答