我想使用循環而左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來獲取鼠標按鈕的當前狀態? –