-3

我有代码,每次发送消息时,它都会对其进行计数,然后设置状态。很简单,但是我在函数中添加了一些消息发送调用,因此它更快,现在显然计数已关闭,因为它在此示例中每四条消息计数一次:

    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,依此类推。

4

3 回答 3

0

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

于 2012-04-14T06:40:07.903 回答
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;
}
于 2012-04-14T06:41:24.290 回答
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;
        }


    });
        }
于 2012-04-14T06:57:29.087 回答