[英]How to stop timer if a Listbox is empty or items are less than 0 in winform applications
I am making a windows app. 我正在制作Windows应用程序。
Here is the code: 这是代码:
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;
}
}
The above code works well till there is one item left in listBox1. 上面的代码可以很好地工作,直到listBox1中只剩下一项为止。
The error is: 错误是:
System.ArgumentOutOfRangeException was unhandled Message=InvalidArgument=Value of '0' is not valid for 'index'. 未处理System.ArgumentOutOfRangeException消息= InvalidArgument =值“ 0”对“索引”无效。 Parameter name: index 参数名称:索引
It raises an error when listBox1 reaches 0. I want to stop the timer when listbox1 is empty or gets no items or 0 items. 当listBox1达到0时,它将引发错误。我想在listbox1为空或没有项目或0项目时停止计时器。
try this 尝试这个
int count = City.Items.Count - 1;
for (int i = count; i > 0; i--){
City.Items.RemoveAt(i);
}
Here is what worked for me. 这对我有用。
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;
}
}
The problem is in this code: 问题出在以下代码中:
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;
}
}
So what is happening is that you are using count to check >0 then calling jabber to do the work, It the call becomes slow- you will see multiple timers getting fired back. 所以发生的事情是,您正在使用count检查> 0,然后调用jabber来完成工作,该调用变得很慢-您将看到多个计时器被触发。 So a big queue will be collected there. 因此,那里将排起长队。 You need to modify the code a bit here using lock to hold the list and allow jabber to do its work: 您需要在此处使用锁来修改代码,以保留列表并允许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 will also ensure that the items are removed correctly. 锁也将确保正确删除项目。 To save the file as per comment below : 要按照以下注释保存文件:
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();
}
}
Now we need to set in form like this: 现在我们需要设置如下形式:
private void Form1_Load(object sender, EventArgs e)
{
ChatHistoryManager chatHistoryManager = new ChatHistoryManager(this.richTextBox1);
chatHistoryManager.Location = @"C:\Development\test.txt";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.