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

本文关键字:小于 何停止 计时器 应用程序 winform 如果 列表 | 更新日期: 2023-09-27 18:06:03

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

  • 一个按钮1,从服务器开始获取listBox1中的项目。
  • 启动定时器的按钮2。
  • 从listBox1中删除项的计时器。
  • 显示进程进度的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中还剩下一个项目。

错误是:

系统。未处理ArgumentOutOfRangeExceptionMessage=InvalidArgument= '0'的值对'index'无效。参数名称:index

当listBox1达到0时抛出错误。我想在listbox1为空或没有项或0项时停止计时器

在winform应用程序中,如果列表框为空或项小于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检查>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;
            }
        }
    }

Lock也将确保项目被正确删除。按照下面的注释保存文件:

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

try this

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

我是这么做的。

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