繁体   English   中英

程序死机且计时器循环无休止

[英]Program freezes and timer loop is endless

我的程序将“ Z”键绑定到激活计时器的处理程序。 该计时器触发鼠标单击。

问题是,如果我按住Z超过5秒钟,它会卡住,并且在KeyUp上不会触发,变量不会更改为false并且循环是无限的, 因此当不再按下键时它将继续触发计时器的回调 阻止它的唯一方法是通过ALT+F4

我的代码在http://pastebin.com/rbCgY1rb

从这里使用globalKeyboardHook

代码的关键部分是:

 private void keyDownCallback(object sender, KeyEventArgs e) {
                    if (e.KeyCode.ToString() == "Z") {
                            timer1.Enabled = true;
                            this.forceNoLoop = false;
                    } else if(e.KeyCode.ToString() == "X") {
                            timer1.Enabled = false;
                            this.forceNoLoop = true;
                    }
            }

            private void keyUpCallback(object sender, KeyEventArgs e) {
                    timer1.Enabled = false;
                    this.forceNoLoop = true;
            }

            private void timer1_Tick(object sender, EventArgs e) {
                    if (forceNoLoop) return;
        mouse_event(MOUSEEVENTF_LEFTDOWN, (uint)Cursor.Position.X, (uint)Cursor.Position.Y, 0, 0);
        mouse_event(MOUSEEVENTF_LEFTUP, (uint)Cursor.Position.X, (uint)Cursor.Position.Y, 0, 0);
                    lblClickStatus.Text = "clicked " + (this.clickTimes++) + " times";
            }

所以问题是:如何解决冻结问题?

您可以在启用/禁用计时器之前尝试检查其状态吗?

private void keyDownCallback(object sender, KeyEventArgs e) {
    if (e.KeyCode.ToString() == "Z") {
        if (!timer1.Enabled)
            timer1.Enabled = true;
    } else if (e.KeyCode.ToString() == "X") {
        if (timer1.Enabled)
            timer1.Enabled = false;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM