我正在尝试创建一个每隔10秒就会输入一条消息的自动轮询器。 它应该发送总共28条消息。每条消息都在MultiLine文本框中的不同行上。
它有以下布局:
我合并了一些我在网上找到的代码。 它可能看起来很愚蠢。但我还是初学者:)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AuthKeyTyper
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label2_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void StartBtn_Click(object sender, EventArgs e)
{
TimerAuth.Enabled = true;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void StopBtn_Click(object sender, EventArgs e)
{
TimerAuth.Enabled = false;
}
private void TimerAuth_Tick(object sender, EventArgs e)
{
TextReader read = new System.IO.StringReader(TextBox.Text);
int rows = 27;
string[] text1 = new string[rows];
for (int r = 0; r < rows; r++)
{
text1[r] = read.ReadLine();
}
SendKeys.Send("::auth " + text1[0] + "{enter}");
TimerAuth.Enabled = false;
TimerAuth.Enabled = true;
SendKeys.Send("::auth " + text1[1] + "{enter}");
TimerAuth.Enabled = false;
TimerAuth.Enabled = true;
SendKeys.Send("::auth " + text1[2] + "{enter}");
TimerAuth.Enabled = false;
TimerAuth.Enabled = true;
SendKeys.Send("::auth " + text1[3] + "{enter}");
TimerAuth.Enabled = false;
TimerAuth.Enabled = true;
SendKeys.Send("::auth " + text1[4] + "{enter}");
TimerAuth.Enabled = false;
TimerAuth.Enabled = true;
SendKeys.Send("::auth " + text1[5] + "{enter}");
TimerAuth.Enabled = false;
TimerAuth.Enabled = true;
SendKeys.Send("::auth " + text1[6] + "{enter}");
TimerAuth.Enabled = false;
TimerAuth.Enabled = true;
SendKeys.Send("::auth " + text1[7] + "{enter}");
TimerAuth.Enabled = false;
TimerAuth.Enabled = true;
SendKeys.Send("::auth " + text1[8] + "{enter}");
TimerAuth.Enabled = false;
TimerAuth.Enabled = true;
SendKeys.Send("::auth " + text1[9] + "{enter}");
TimerAuth.Enabled = false;
TimerAuth.Enabled = true;
SendKeys.Send("::auth " + text1[10] + "{enter}");
TimerAuth.Enabled = false;
TimerAuth.Enabled = true;
SendKeys.Send("::auth " + text1[11] + "{enter}");
TimerAuth.Enabled = false;
TimerAuth.Enabled = true;
SendKeys.Send("::auth " + text1[12] + "{enter}");
TimerAuth.Enabled = false;
TimerAuth.Enabled = true;
SendKeys.Send("::auth " + text1[13] + "{enter}");
TimerAuth.Enabled = false;
TimerAuth.Enabled = true;
SendKeys.Send("::auth " + text1[14] + "{enter}");
TimerAuth.Enabled = false;
TimerAuth.Enabled = true;
SendKeys.Send("::auth " + text1[15] + "{enter}");
TimerAuth.Enabled = false;
TimerAuth.Enabled = true;
SendKeys.Send("::auth " + text1[16] + "{enter}");
TimerAuth.Enabled = false;
TimerAuth.Enabled = true;
SendKeys.Send("::auth " + text1[17] + "{enter}");
TimerAuth.Enabled = false;
TimerAuth.Enabled = true;
SendKeys.Send("::auth " + text1[18] + "{enter}");
TimerAuth.Enabled = false;
TimerAuth.Enabled = true;
SendKeys.Send("::auth " + text1[19] + "{enter}");
TimerAuth.Enabled = false;
TimerAuth.Enabled = true;
SendKeys.Send("::auth " + text1[20] + "{enter}");
TimerAuth.Enabled = false;
TimerAuth.Enabled = true;
SendKeys.Send("::auth " + text1[21] + "{enter}");
TimerAuth.Enabled = false;
TimerAuth.Enabled = true;
SendKeys.Send("::auth " + text1[22] + "{enter}");
TimerAuth.Enabled = false;
TimerAuth.Enabled = true;
SendKeys.Send("::auth " + text1[23] + "{enter}");
TimerAuth.Enabled = false;
TimerAuth.Enabled = true;
SendKeys.Send("::auth " + text1[24] + "{enter}");
TimerAuth.Enabled = false;
TimerAuth.Enabled = true;
SendKeys.Send("::auth " + text1[25] + "{enter}");
TimerAuth.Enabled = false;
TimerAuth.Enabled = true;
SendKeys.Send("::auth " + text1[26] + "{enter}");
TimerAuth.Enabled = false;
TimerAuth.Enabled = true;
SendKeys.Send("::auth " + text1[27] + "{enter}");
TimerAuth.Enabled = false;
MessageBox.Show("Done");
}
}
}
答案 0 :(得分:-1)
首先关闭。感谢所有的选票......我说我是初学者,我有兴趣学习C#。经过一些挖掘和阅读后,我发现了“等待”的功能。
所以我摆脱了计时器并将整个函数放入启动button_click事件并使用异步。
所以这成了sendkey事件的形式:
SendKeys.Send(...)
await Task.Delay(10000)
SendKeys.Send(...)
await Task.Delay(10000)
享受!