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が変更されると、null値または例外が発生する可能性があります。非常にまれです。

編集

そして、逆の方向に進むのはさらに簡単です。

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 に答える