对于C#中的每个循环计数

时间:2012-04-14 06:36:30

标签: c# loops count

我有代码,每次发送消息时,它会对其进行计数,然后设置状态。很简单,但我已经添加了一些调用消息发送到函数的更多调用,所以它更快,现在显然计数已关闭,因为它在这个例子中计算每四个消息:

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

我希望上面的代码在每次发送消息时计算:

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

当发送消息1时,它应该将状态设置为1,然后当发送消息2时,应将状态设置为2等,并从那里开始。

3 个答案:

答案 0 :(得分:0)

在返回int的方法中包装skype.SendMessage,其最后一行将返回1;。将int添加到新的变量计数中。当它抛出时,count将有成功的消息发送计数

答案 1 :(得分:0)

为什么不直接调整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;
}

答案 2 :(得分:0)

我认为这应该满足您的要求......

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


    });
        }