为什么我的代码只对击键响应两次?
我正在撰写C#中的应用程序,该应用程序检查键盘是否暂停或通过Naudio播放音乐。我有一个以另一种形式寻找击键的循环,但是由于某种原因,它不会打破暂停音乐并防止两次击键后的另一个循环(这意味着它可以重播音乐)。我已经使用断点进行了调试,并查看了Autos,以查看我是否正确分配了变量,但是由于某种原因,它只是在我按A和S后一次没有执行任何代码(其中有问题的击键)。
这是查找关键触发器并分配变量的代码:
private void Form2_KeyDown(object sender, KeyEventArgs e)
{
Task.Delay(100);
if (e.KeyCode == Keys.S)
{
stgiga.Class1.starting = true; //triggers music
Class1.pressed = true; //indicates keypress to Form1
Task.Delay(1000);
Class1.pressed = false; //switches to false to stop looping
}
if (e.KeyCode == Keys.A)
{
Class1.playing = false; //stops music
Class1.pressed = true; //indicates keypress to Form1
Task.Delay(1000);
Class1.pressed = false; //prevents looping
var p = new Process();
p.StartInfo.FileName = @"C:\Program Files\REAPER (x64)\reaper.exe"; //unrelated
p.Start();
}
}
这是寻找变量变化并适当响应的循环:
while (true)
{
if (stgiga.Class1.pressed == true & stgiga.Class1.playing == false)
{
output.Stop(); //Stop audio via. NAudio
while(stgiga.Class1.starting == false ) //waits until starting = true to make playing = true so it doesn't loop through twice and play
{
}
stgiga.Class1.playing = true;
}
if (stgiga.Class1.starting == true & stgiga.Class1.pressed == true)
{
main(); //Main is another loop that should play audio
stgiga.Class1.starting = false;
}
}
最后,这应该打破Main()循环,但在两次击键后失败:
if (stgiga.Class1.playing == false)
{
output.Stop();
break;
}
谢谢您。
I'm writing an application in C# that checks for keystrokes to pause or play music via NAudio. I have a loop that looks for a keystroke in another form, but for some reason it does not break to pause the music and prevent another loop (which means it replays music) after two keystrokes. I have debugged using breakpoints and looked at autos to see if I'm assigning variables correctly, but for some reason it just simply does not execute any code after I press A once and S (the keystrokes in question) once.
Here is the code which looks for key triggers and assigns variables:
private void Form2_KeyDown(object sender, KeyEventArgs e)
{
Task.Delay(100);
if (e.KeyCode == Keys.S)
{
stgiga.Class1.starting = true; //triggers music
Class1.pressed = true; //indicates keypress to Form1
Task.Delay(1000);
Class1.pressed = false; //switches to false to stop looping
}
if (e.KeyCode == Keys.A)
{
Class1.playing = false; //stops music
Class1.pressed = true; //indicates keypress to Form1
Task.Delay(1000);
Class1.pressed = false; //prevents looping
var p = new Process();
p.StartInfo.FileName = @"C:\Program Files\REAPER (x64)\reaper.exe"; //unrelated
p.Start();
}
}
Here is the loop which looks for changes in variables and responds appropriately:
while (true)
{
if (stgiga.Class1.pressed == true & stgiga.Class1.playing == false)
{
output.Stop(); //Stop audio via. NAudio
while(stgiga.Class1.starting == false ) //waits until starting = true to make playing = true so it doesn't loop through twice and play
{
}
stgiga.Class1.playing = true;
}
if (stgiga.Class1.starting == true & stgiga.Class1.pressed == true)
{
main(); //Main is another loop that should play audio
stgiga.Class1.starting = false;
}
}
Lastly, this is what's supposed to break the main() loop but fails after two keystrokes:
if (stgiga.Class1.playing == false)
{
output.Stop();
break;
}
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从哪里开始...不要做无限的循环?
您的循环可能在UI线程中,因此当然会停止输入。但是,也许您在另一个线程中有。
即使在另一个线程中,它也无法很好地为您服务。您的CPU使用率将达到100%。您绝对不应该只是出于一些原因检查变量尽可能快地循环。首先,优化器可能只是优化了支票,因为循环内部的任何内容都无法更改值。但是,此外,只需可怕地运行您的线程,并且无法将其用于其他任何东西。
task.delay
计划在给定时间之后完成的任务。但是,您实际上并没有等待完成该任务。当发生变化时,您需要发射某种事件,然后响应这些事件。不要在寻找变化的东西上循环。
要使用事件,您将声明事件处理程序:
public Event Handler MusicStarting
;然后,您将注册以处理该事件:
当然,当您启动音乐时,您需要触发事件:
您可以获得更奇特的,请将参数传递给处理程序以及所有这些。
Where to begin....Don't make infinite loops?
Likely, your loop is in the UI thread, so of course it stops getting input. But, maybe you have that in another thread.
Even in another thread, it won't serve you well. Your CPU usage will hit 100%. You should never just loop as fast as you can checking a variable for a few reason. First, the optimizer might just optimize out the check, since nothing inside the loop can change the value. But, also, just horribly runs your thread and makes it unable to be used for anything else.
Task.Delay
schedules a task that will complete after the given time. But, you don't actually WAIT for that task to complete.You need to fire some kind of event when changes happen, and then respond to those events. Don't loop on something looking for changes.
To use events, you would declare the event handler:
public event EventHandler MusicStarting
;you would then register to handle that event:
and, of course, you need to trigger the event when you start the music:
You can get a lot fancier, pass parameters to the handler and all that.