C# 对于C中的每个循环计数
我有一个代码,每次发送一条消息时,它都会对其进行计数,然后设置状态。很简单,但我在函数中添加了更多的消息发送调用,因此速度更快,现在很明显计数已关闭,因为在本例中,它每四条消息计数一次:C# 对于C中的每个循环计数,c#,loops,count,C#,Loops,Count,我有一个代码,每次发送一条消息时,它都会对其进行计数,然后设置状态。很简单,但我在函数中添加了更多的消息发送调用,因此速度更快,现在很明显计数已关闭,因为在本例中,它每四条消息计数一次: private void messagespam_worker_DoWork(object sender, DoWorkEventArgs e) { for (int i = 1; i < numericUpDown1.Value; i++) Inv
private void messagespam_worker_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 1; i < numericUpDown1.Value; i++)
Invoke((MethodInvoker)delegate
{
foreach (Chat chat in skype.Chats)
{
if (messagespam_bool == false)
{
numericUpDown1.Value = 0;
break;
}
try
{
toolStripStatusLabel3.Text = "- Sent: " + i; // Where the status is changed
String contact = listBox1.SelectedItem.ToString();
skype.SendMessage(contact, textBox7.Text); //1st message
skype.SendMessage(contact, textBox7.Text); //2nd message
skype.SendMessage(contact, textBox7.Text); //3rd message
skype.SendMessage(contact, textBox7.Text); //4th message
}
catch (Exception error)
{
MessageBox.Show(error.Message);
}
break;
}
});
}
发送消息1时,应将状态设置为1,然后发送消息2时,应将状态设置为2,以此类推。用返回int的方法包装skype.SendMessage,最后一行为return 1;。将int添加到新的变量count。当它抛出count时,将有成功消息发送的计数为什么不直接调整SendMessage方法?将其替换为您自己执行计数/状态更新的方法:
private int count;
private void SendAndCount(Contact contact, String text)
{
// add your count logic here
count++;
skype.SendMessage(contact, textBox7.Text);
}
然后更新您的循环:
foreach (Chat chat in skype.Chats)
{
if (messagespam_bool == false)
{
numericUpDown1.Value = 0;
break;
}
try
{
String contact = listBox1.SelectedItem.ToString();
SendAndCount(contact, textBox7.Text); //1st message
SendAndCount(contact, textBox7.Text); //2nd message
SendAndCount(contact, textBox7.Text); //3rd message
SendAndCount(contact, textBox7.Text); //4th message
}
catch (Exception error)
{
MessageBox.Show(error.Message);
}
break;
}
我想这应该能满足你的要求
private void messagespam_worker_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 1; i < numericUpDown1.Value; i++)
Invoke((MethodInvoker)delegate
{
foreach (Chat chat in skype.Chats)
{
if (messagespam_bool == false)
{
numericUpDown1.Value = 0;
break;
}
try
{
toolStripStatusLabel3.Text = "- Sent: " + i*4; // 4 is the number of messages, better keep it in a variable
String contact = listBox1.SelectedItem.ToString();
skype.SendMessage(contact, textBox7.Text); //1st message
toolStripStatusLabel3.Text = "- Sent: " + i*4 + 1; // remove this if this is not required
skype.SendMessage(contact, textBox7.Text); //2nd message
toolStripStatusLabel3.Text = "- Sent: " + i*4 + 2;
skype.SendMessage(contact, textBox7.Text); //3rd message
toolStripStatusLabel3.Text = "- Sent: " + i*4 + 3;
skype.SendMessage(contact, textBox7.Text); //4th message
}
catch (Exception error)
{
MessageBox.Show(error.Message);
}
break;
}
});
}
BackgroundWorker是否正在调用此方法?如果是这样,你不应该更新用户界面!toolStripStatusLabel3.Text将失败。@ColinE请阅读代码。它被明确地调用了,我使用了这段代码,并且我声明它显然工作得很好。请在发帖前阅读整篇文章,不要浏览。好吧,我试过了,然后对发送到skype的所有邮件进行了一行计数,其中96条邮件已发送,状态为read sent-1283,让它像冠军一样工作,谢谢,太多人了,所有其他的答案都是疯狂的,让我重编了一大堆废话,这只是在结尾处增加了i@JognSmith考虑到这是一个社区网站,人们不需要付费来写答案,你可以对试图帮助你的人礼貌一点。
private void messagespam_worker_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 1; i < numericUpDown1.Value; i++)
Invoke((MethodInvoker)delegate
{
foreach (Chat chat in skype.Chats)
{
if (messagespam_bool == false)
{
numericUpDown1.Value = 0;
break;
}
try
{
toolStripStatusLabel3.Text = "- Sent: " + i*4; // 4 is the number of messages, better keep it in a variable
String contact = listBox1.SelectedItem.ToString();
skype.SendMessage(contact, textBox7.Text); //1st message
toolStripStatusLabel3.Text = "- Sent: " + i*4 + 1; // remove this if this is not required
skype.SendMessage(contact, textBox7.Text); //2nd message
toolStripStatusLabel3.Text = "- Sent: " + i*4 + 2;
skype.SendMessage(contact, textBox7.Text); //3rd message
toolStripStatusLabel3.Text = "- Sent: " + i*4 + 3;
skype.SendMessage(contact, textBox7.Text); //4th message
}
catch (Exception error)
{
MessageBox.Show(error.Message);
}
break;
}
});
}