所以现在我有了这个:钩住了热键和一个volatile bool
正在检查while循环。
UserActivityHook actHook;
private void Form1_Load(object sender, EventArgs e)
{
actHook = new UserActivityHook();
actHook.KeyPress += new KeyPressEventHandler(klawisze);
actHook.KeyDown += new KeyEventHandler(klawisze2);
actHook.Start();
}
public void pw()
{
while (!stopIt)
{
SetCursorPos(576, 363);
Thread.Sleep(50);
SetCursorPos(650, 363);
Thread.Sleep(50);
SetCursorPos(650, 430);
Thread.Sleep(50);
}
}
public void Stop()
{
stopIt = true;
}
private volatile bool stopIt;
public void dw()
{
while (!stopIt)
{
SetCursorPos(860, 350);
Thread.Sleep(50);
SetCursorPos(890, 350);
Thread.Sleep(50);
SetCursorPos(890, 380);
Thread.Sleep(50);
}
}
public void klawisze(object sender, KeyPressEventArgs e)
{
Thread w1 = new Thread(new ThreadStart(pw));
Thread w2 = new Thread(new ThreadStart(dw));
try
{
if (e.KeyChar == 21) //CTRL + SHIFT + U
{ //1
stopIt = false;
w1.Start();
if (e.KeyChar == 9)
{
Stop();
w1.Join();
}
}
if (e.KeyChar == 9) //CTRL + SHIFT + I
{ //2
stopIt = false;
w2.Start();
if (e.KeyChar == 21)
{
Stop();
w2.Join();
}
}
}
当我按下第一个热键然后第二个同时绘制2个三角形时,它不会关闭第一个绘图并生效。这有什么不对?
答案 0 :(得分:0)
我希望这是你问题的答案。你应该创建另一个线程,否则这将无法正常工作。但是,我会将winform中的布尔值“firstLoop”发送到您的其他线程。我的解决方案不是100%正确...
Boolean firstLoop = false;
if (e.KeyChar == 21) //CTRL + SHIFT + U
{
firstLoop = true
while (firstLoop == true)
{
SetCursorPos(576, 363);
Thread.Sleep(50);
SetCursorPos(650, 363);
Thread.Sleep(50);
SetCursorPos(650, 430);
Thread.Sleep(50);
}
}
if (e.KeyChar == 9) //CTRL + SHIFT + I
{
firstLoop = false;
while (firstLoop == false)
{
SetCursorPos(860, 350);
Thread.Sleep(50);
SetCursorPos(890, 350);
Thread.Sleep(50);
SetCursorPos(890, 380);
Thread.Sleep(50);
}
}