-3

メッセージが送信されるたびにそれをカウントしてステータスを設定するコードがあります。シンプルですが、関数にメッセージを送信するための呼び出しをいくつか追加したため、高速になり、この例では 4 つのメッセージごとにカウントされているため、明らかにカウントがオフになっています。

    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

int を返すメソッドで skype.SendMessage をラップし、その最後の行は return 1; になります。int を新しい変数 count に追加します。count をスローすると、成功したメッセージ送信の数がカウントされます

于 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 に答える