gpt4 book ai didi

按下 mousedown 按钮时 C# 循环

转载 作者:太空宇宙 更新时间:2023-11-03 11:31:12 25 4
gpt4 key购买 nike

我想在按下鼠标左键时使用循环:

  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 事件永远不会触发。

那么如何让它正常工作呢?

最佳答案

使用 BackGroundWorker。非常适合您的问题。

将循环函数放入 worker 并根据鼠标事件启动/停止 worker。

关于按下 mousedown 按钮时 C# 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7712309/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com