我想使用循环而左mousebutton按下:C#循环,当鼠标按下按钮按下
private void Loop_MouseDown(object sender, MouseEventArgs e)
{
while (e.Button==MouseButtons.Left)
{
//Loop
}
}
我不能从这个线程使用的解决方案:
C# how to loop while mouse button is held down 因为我通过RS232发送数据和使用它自己的时间间隔使用计时器不起作用。此外,这个主题的任何解决方案都不适合我。 它不能工作,也像一个在这里:
if (e.Button == MouseButtons.Left)
{
//loop
}
该解决方案还不起作用:
bool isLooping = false;
//on mouse down
private void myControl_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) {
isLooping = true;
runLoop();
}
//on mouse up event
private void myControl_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) {
isLooping = false;
}
//This is the main loop you care about. Put this in your application
//This should go in its own thread
void runLoop() {
while (isLooping) {
//do stuff
}
}
,因为调用runLoop会阻塞线程,因此MouseUp事件永远不会火。
那么如何使它正常工作?
你为什么要这样? – TJHeuvel
为什么计时器不工作? – SLaks
您是否考虑过使用API来获取鼠标按钮的当前状态? –