如果列表框为空或winform应用程序中的项目小于0,如何停止计时器

时间:2014-06-01 12:57:32

标签: c# winforms

我正在制作一个Windows应用程序。

  • 一个button1,它在开始时从服务器获取listBox1中的项目。
  • 启动timer1的按钮2。
  • 从listBox1中删除项目的timer1。
  • progressBar1,显示此过程的进度。

以下是代码:

private void button1_Click(object sender, EventArgs e)
{
    jabber.Send("<iq type='get' to='" + textBox1.Text + "@conference.jabber.com' id='qip_1026'><query xmlns='http://jabber.org/protocol/muc#admin'><item affiliation='outcast' /></query></iq>");
}

private void button2_Click(object sender, EventArgs e)
{
    progressBar1.Maximum = listBox1.Items.Count;
    timer1.Start();
    timer1.Interval = 4000;
}

private void timer1_Tick(object sender, EventArgs e)
{
    if (listBox1.Items.Count > 0)
    {
        jabber.Send("<iq type='set' to='" + textBox7.Text + "@conference.jabber.com'><query xmlns='http://jabber.org/protocol/muc#admin'><item jid='" + listBox1.Items[0].ToString() + "' affiliation='none'/></query></iq>");
        listBox1.Items.RemoveAt(0);
        progressBar1.Value += 1;
        label.Text = listBox1.Items.Count.ToString();
    }
    else
    {
        timer1.Enabled = False;
    }
}

上面的代码很有效,直到listBox1中还有一个项目。

错误是:

System.ArgumentOutOfRangeException未处理 Message = InvalidArgument =&#39; 0&#39;的值对于&#39; index&#39;无效。 参数名称:index

当listBox1达到0时会引发错误。我想在listbox1为空或没有项目或0项时停止计时器。

3 个答案:

答案 0 :(得分:0)

试试这个

            int count = City.Items.Count - 1;
            for (int i = count; i > 0; i--){
                City.Items.RemoveAt(i);
               }

答案 1 :(得分:0)

这对我有用。

private void button1_Click(object sender, EventArgs e)
{
jabber.Send("<iq type="get" to="" + textBox1.Text + "@conference.jabber.com" id="qip_1026"><query xmlns="http://jabber.org/protocol/muc#admin"><item affiliation="outcast" /></query></iq>");
}

private void button2_Click(object sender, EventArgs e)
{
progressBar1.Maximum = listBox1.Items.Count;
progressBar1.Value = 0;
// Set the timer interval to four seconds
timer1.Interval = 4000;
// Start the timer
timer1.Start();
}

private void timer1_Tick(object sender, EventArgs e)
 {
// Disable the timer while we are working in this procedure so
// it doesn't tick while we are working in this procedure
timer1.Enabled = False;
// Send only if there are items in the ListBox
if (listBox1.Items.Count > 0)
{
    jabber.Send("<iq type="set" to="" + textBox7.Text + "@conference.jabber.com"><query xmlns="http://jabber.org/protocol/muc#admin"><item jid="" + listBox1.Items[0].ToString() + "" affiliation="none" /></query></iq>");
    listBox1.Items.RemoveAt(0);
    progressBar1.Value += 1;
    label.Text = listBox1.Items.Count.ToString();
}
// Re-enable only if there are items left in the ListBox
if (listBox1.Items.Count > 0)
{
     timer1.Enabled = True;
}
 }

答案 2 :(得分:0)

问题在于此代码:

  private void timer1_Tick(object sender, EventArgs e)
    {
        if (listBox1.Items.Count > 0)
        {
            jabber.Send("<iq type='set' to='" + textBox7.Text + "@conference.jabber.com'><query xmlns='http://jabber.org/protocol/muc#admin'><item jid='" + listBox1.Items[0].ToString() + "' affiliation='none'/></query></iq>");
            listBox1.Items.RemoveAt(0);
            progressBar1.Value += 1;
            label.Text = listBox1.Items.Count.ToString();
        }
        else
        {
            timer1.Enabled = False;
        }
    }

所以发生的事情是你使用count检查&gt; 0然后调用jabber来完成工作,调用变慢 - 你会看到多个计时器被反击。因此,那里将收集一大队。你需要在这里修改一下代码,使用lock来保存列表并允许jabber完成它的工作:

private void timer1_Tick(object sender, EventArgs e)
    {
        lock (listBox1)
        {
            if (listBox1.Items.Count > 0)
            {
                jabber.Send("<iq type='set' to='" + textBox7.Text +
                            "@conference.jabber.com'><query xmlns='http://jabber.org/protocol/muc#admin'><item jid='" +
                            listBox1.Items[0].ToString() + "' affiliation='none'/></query></iq>");
                listBox1.Items.RemoveAt(0);
                progressBar1.Value += 1;
                label.Text = listBox1.Items.Count.ToString();
            }
            else
            {
                timer1.Enabled = False;
            }
        }
    }

锁定还可确保正确删除项目。 要根据以下评论保存文件:

 public class ChatHistoryManager
{
    private readonly RichTextBox richTextBox;
    private Timer timer = new Timer();
    public ChatHistoryManager(RichTextBox richTextBox)
    {
        this.richTextBox = richTextBox;
        this.InitializeTimer();
    }

    public string Location { get; set; }
    private void InitializeTimer()
    {
        this.timer.Tick += timer_Tick;
        this.timer.Enabled = true;          
        this.timer.Interval = (int) TimeSpan.FromHours(1).TotalMilliseconds;
    }

    void timer_Tick(object sender, EventArgs e)
    {
        this.SaveFile();
    }

    public void SaveFile()
    {
        //Save the file to the location
        this.richTextBox.SaveFile(this.Location,RichTextBoxStreamType.RichText);
    }

    public void Stop()
    {
        this.timer.Stop();
    }
}

现在我们需要设置如下形式:

  private void Form1_Load(object sender, EventArgs e)
    {
        ChatHistoryManager chatHistoryManager = new ChatHistoryManager(this.richTextBox1);
        chatHistoryManager.Location = @"C:\Development\test.txt";
    }