【问题标题】:C# listbox continuous loopC#列表框连续循环
【发布时间】:2011-07-05 09:51:23
【问题描述】:

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

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# loops listbox continuous


    【解决方案1】:

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

    你的 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)
        {
            ...
        }  
    }
    

    【讨论】:

    • 列表框不应该经常更改,我现在唯一的问题是循环从列表框中的最后一项开始并向后移动......
    • @Lyndys:那么你就不会经常遇到问题了。而且你将它编程为倒退,每个人都在经历额外的麻烦来保持这种状态......
    • 我想这是我能想到的唯一方式...我似乎无法以其他方式...
    • 你给的这个代码..我必须启动它两次..不知道为什么:))
    【解决方案2】:

    这样的?

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

    【讨论】:

    • 请注意@Henk 在他的回答中所说的话。 WinForms 中的多线程需要注意一下。
    • 知道为什么列表会在循环中向后加载吗?从头到尾?
    • @Lindys:因为 for 循环从项目倒数到 0,所以将其更改为 for (int i = 0; i &lt; listBox1.Items.Count; i++) 它将继续进行
    【解决方案3】:
    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;
    

    【讨论】:

    • 我使用带有停止按钮的取消,所以我不想搞砸它,关于报告进度......我可能会保持原样,谢谢你的回答
    【解决方案4】:
    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;
        }
    

    【讨论】:

    • 您是否注意到此代码的工作方式与完全相同不同?在CancellationPending的情况下,将处理所有剩余的项目,然后作业将停止
    • 好地方,但我马上更新了...,所以现在我看不出有什么不同
    • 现在行为没有区别了:)
    猜你喜欢
    • 2020-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-11
    • 1970-01-01
    • 2011-08-03
    • 1970-01-01
    相关资源
    最近更新 更多