跳出 while 循环并进入睡眠状态
Breaking out of while loop and sleep
我正在尝试用 C# 制作一个非常简单的自动点击器,只是为了更好地使用它。
我有一个包含两个数字文本框和两个按钮的表单。第一个文本框指定每次点击之间的时间间隔(以毫秒为单位),第二个文本框指定迭代次数。
我的第一个按钮是 button1
,它实际上只是启动程序。我有一个名为 button2
的第二个按钮,它将停止 button1_Click
功能。
这是我拥有的:
private void button1_Click(object sender, EventArgs e)
{
//While a is less than number of specified iterations
for (int a = 0; a < Convert.ToInt32(numericUpDown2.Value); a++)
{
//Sleep for desired time
System.Threading.Thread.Sleep(Convert.ToInt32(numericUpDown1.Value));
//Get x/y coordinates of mouse
int X = Cursor.Position.X;
int Y = Cursor.Position.Y;
//Click mouse at x/y coordinates
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
}
}
public void Form_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
this.Close();
}
}
我的问题是我的程序卡在了那个for循环中,我没有办法中断button1_Click
函数。
我想要这样,如果我按 F11 或我的 button2
按钮,button1_Click
功能将立即停止,但表单本身仍会打开。现在我只是为了简单起见而使用 ESC
键。
像这样
public Timer myTimer { get; set; }
public Form()
{
myTimer = new Timer();
myTimer.Tick += new EventHandler(TimerEventProcessor);
}
private void button1_Click(object sender, EventArgs e)
{
myTimer.Interval = Convert.ToInt32(numericUpDown1.Value);
myTimer.Start();
}
public void Form_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
myTimer.Stop();
this.Close();
}
}
private static void TimerEventProcessor(Object myObject, EventArgs myEventArgs)
{
//Get x/y coordinates of mouse
int X = Cursor.Position.X;
int Y = Cursor.Position.Y;
//Click mouse at x/y coordinates
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
}
向您的表单添加一个 'Timer' 对象,将其间隔设置为 'sleep' 的毫秒数(不是真的)。将其启用 属性 设置为 false。
处理它的 Tick 事件并将所有内循环代码放在那里(mouse_event 调用)。
在 button1_Click(buttonStart 会是一个更好的名称)中,将计时器启用设置为真,并在 button2_Click (buttonStop) 中将计时器启用设置为假。
Timer t1;
int ticks = 0;
bool timerInitialized = false
private void button1_Click(object sender, EventArgs e)
{
if (!timerInitialized)
{
t1 = new Timer();
t1.Tick += Timer_Tick;
timerInitialized = true;
}
button1.Enabled = false;
t1.Interval = Convert.ToInt32(numericUpDown1.Value);
ticks = 0;
t1.Start();
}
private void Timer_Tick(object sender, EventArgs e)
{
if (ticks < Convert.ToInt32(numericUpDown2.Value))
{
ticks++;
//Get x/y coordinates of mouse
int X = Cursor.Position.X;
int Y = Cursor.Position.Y;
//Click mouse at x/y coordinates
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
}
else
{
t1.Stop(); //You could Call Stop from every where e.g. from another Button
button1.Enabled = true;
}
}
编辑:
仅分配一次 "Tick" 事件
我正在尝试用 C# 制作一个非常简单的自动点击器,只是为了更好地使用它。
我有一个包含两个数字文本框和两个按钮的表单。第一个文本框指定每次点击之间的时间间隔(以毫秒为单位),第二个文本框指定迭代次数。
我的第一个按钮是 button1
,它实际上只是启动程序。我有一个名为 button2
的第二个按钮,它将停止 button1_Click
功能。
这是我拥有的:
private void button1_Click(object sender, EventArgs e)
{
//While a is less than number of specified iterations
for (int a = 0; a < Convert.ToInt32(numericUpDown2.Value); a++)
{
//Sleep for desired time
System.Threading.Thread.Sleep(Convert.ToInt32(numericUpDown1.Value));
//Get x/y coordinates of mouse
int X = Cursor.Position.X;
int Y = Cursor.Position.Y;
//Click mouse at x/y coordinates
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
}
}
public void Form_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
this.Close();
}
}
我的问题是我的程序卡在了那个for循环中,我没有办法中断button1_Click
函数。
我想要这样,如果我按 F11 或我的 button2
按钮,button1_Click
功能将立即停止,但表单本身仍会打开。现在我只是为了简单起见而使用 ESC
键。
像这样
public Timer myTimer { get; set; }
public Form()
{
myTimer = new Timer();
myTimer.Tick += new EventHandler(TimerEventProcessor);
}
private void button1_Click(object sender, EventArgs e)
{
myTimer.Interval = Convert.ToInt32(numericUpDown1.Value);
myTimer.Start();
}
public void Form_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
myTimer.Stop();
this.Close();
}
}
private static void TimerEventProcessor(Object myObject, EventArgs myEventArgs)
{
//Get x/y coordinates of mouse
int X = Cursor.Position.X;
int Y = Cursor.Position.Y;
//Click mouse at x/y coordinates
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
}
向您的表单添加一个 'Timer' 对象,将其间隔设置为 'sleep' 的毫秒数(不是真的)。将其启用 属性 设置为 false。
处理它的 Tick 事件并将所有内循环代码放在那里(mouse_event 调用)。
在 button1_Click(buttonStart 会是一个更好的名称)中,将计时器启用设置为真,并在 button2_Click (buttonStop) 中将计时器启用设置为假。
Timer t1;
int ticks = 0;
bool timerInitialized = false
private void button1_Click(object sender, EventArgs e)
{
if (!timerInitialized)
{
t1 = new Timer();
t1.Tick += Timer_Tick;
timerInitialized = true;
}
button1.Enabled = false;
t1.Interval = Convert.ToInt32(numericUpDown1.Value);
ticks = 0;
t1.Start();
}
private void Timer_Tick(object sender, EventArgs e)
{
if (ticks < Convert.ToInt32(numericUpDown2.Value))
{
ticks++;
//Get x/y coordinates of mouse
int X = Cursor.Position.X;
int Y = Cursor.Position.Y;
//Click mouse at x/y coordinates
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
}
else
{
t1.Stop(); //You could Call Stop from every where e.g. from another Button
button1.Enabled = true;
}
}
编辑: 仅分配一次 "Tick" 事件