C# Spambot重复发送模块无法快速发送消息问题求助
Hey there, let's break down why your bot is waiting the wrong amount of time between messages. From what you described, it sounds like you're accidentally using the window-switching delay (the time you give users to switch to the target app) for the message-to-message interval instead of the intended 500ms. Let's fix that.
Common Root Cause
You probably have a single delay variable or a shared Task.Delay/Thread.Sleep call that's being reused for both the initial switch window wait and the message interval. Since you're targeting .NET Framework 4.6.1, async/await is fully supported—so we'll use that to keep your UI responsive while handling delays correctly.
Corrected Code Example
Here's a cleaned-up version of your logic that separates the two distinct delays clearly:
using System.Threading.Tasks; using System.Windows.Forms; private async void btnStartSpamming_Click(object sender, EventArgs e) { // Validate input for send count if (!int.TryParse(txtSendCount.Text, out int totalMessages)) { MessageBox.Show("Please enter a valid number of messages to send!"); return; } // Get user-defined window switch delay (default to 5 seconds if invalid) int switchDelayMs = 5000; if (int.TryParse(txtSwitchDelay.Text, out int userSwitchDelay)) { switchDelayMs = userSwitchDelay; } // Alert user to switch windows MessageBox.Show($"Switch to your target window within {switchDelayMs / 1000} seconds!"); // Wait for the user to switch (this is your "reserve time") await Task.Delay(switchDelayMs); // Send messages with 500ms interval between each for (int i = 0; i < totalMessages; i++) { // Your existing message send logic (e.g., SendKeys or API call) SendSpamMessage(txtSpamContent.Text); // Only wait 500ms if it's not the last message (avoid extra delay at the end) if (i != totalMessages - 1) { await Task.Delay(500); } } MessageBox.Show("Spamming complete!"); } // Helper function for sending your message private void SendSpamMessage(string message) { // Example using SendKeys (adjust based on your actual implementation) SendKeys.SendWait(message + "{ENTER}"); }
Key Fixes to Note
- Separate Delays: We explicitly split the window-switch delay and message interval into two separate variables, so they don't overwrite each other.
- Async/Await: Using
await Task.Delay()instead ofThread.Sleep()keeps your UI responsive—no frozen buttons while the bot runs. - Last Message Check: We skip the final 500ms delay so the bot doesn't wait unnecessarily after sending the last message.
Quick Troubleshooting Tips
- Double-check your original code to make sure you're not reusing the same delay variable for both purposes.
- If you were using
Thread.Sleep(), replace it withawait Task.Delay()—Thread.Sleep()blocks the entire UI thread, which can cause unexpected timing issues. - Verify that your
textBox6.Visiblecondition (from your partial code snippet) isn't accidentally overriding the delay logic—make sure it only controls visibility, not timing.
内容的提问来源于stack exchange,提问作者DatCodeCycle

