For each loop count in C#

I have code where every time a message is send, it counts it and then sets the status. Simple, but I've added some more calls for the message sending into the function so it's faster and now obviously the count is off as it's counting every four messages in this example:

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

I would like the above code to count each time it sends a message:

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

When message 1 is sent it should set status to 1 then when message 2 is sent should set status to 2, etc. and from there on.

Answers


i think this should satisfy your requirement...

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


    });
        }

Wrap skype.SendMessage in a method that returns an int, the last line of which will be return 1;. Add the int to a new variable count. When it throws count will have the count of successful message sends


Why not adapt the SendMessage method directly? Replacing it with your own method that performs your count / status update:

private int count;

private void SendAndCount(Contact contact, String text)
{
   // add your count logic here
   count++;
   skype.SendMessage(contact, textBox7.Text); 
}

Then update your loop:

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

Need Your Help

iPhone Simulator location

iphone objective-c ios xcode ios-simulator

Where on my machine is the iPhone simulator installed?

RoR Tutorial Hartl Chapter 9.3

ruby-on-rails ruby

I got really stuck with the tutorial and have been slowly pulling it back into shape. (I'm a 2 week newbie). When I run