单个事件不必要地执行两次! [英] single event execute unwantedly two time!!!

查看:81
本文介绍了单个事件不必要地执行两次!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么会发生这种情况



i写了一个代码来使用热键来切换vlc的播放暂停,并且按下热键它会重复两次并因此无效结果。

  private   void  hotKeyPlayPause_HotKeyPressed( 对象发​​件人,SmartHotKey.HotKeyEventArgs e)
{
如果(videoLoaded)
{
if (播放)
{
btnPause.Visible = false ;
btnPlay.Visible = true ;
playing = false ;
axVLCPlugin1.pause();
return ;
}
else
{
btnPause.Visible = true ;
btnPlay.Visible = false ;
axVLCPlugin1.play();
playing = true ;
return ;
}
}
}





当我尝试调试它并创建一个断点并关注F11它显示它首先执行pause()动作直到if块返回然后跳回来检查是否(VideoLoaded)阻止...再次输入if / else块以切换播放暂停并且通常这次它将执行其他块作为播放标志在第二次执行时是假的...



关于这个bug的任何想法???我错过了什么???

解决方案

你看,你的陈述不准确。您不能说您的事件执行了两次(事件不执行)。你看到的是你的事件处理程序方法被调用了两次,但这只是一个方法,可以通过不同的原因调用。



首先,检查事件处理程序添加到事件实例的调用列表的位置,即所有+ =运算符。在处理程序的第一个语句上放置一个断点并在调试器下运行它。当执行到断点时,请参阅调用堆栈窗口(在调试菜单下)。它将显示两种情况下来电的来源。



-SA


Be小心你对哪些事件做出反应:有时对原始事件做出反应时对其他事件做出反应可能会引起混淆。

我认为 axVLCPlugin1.pause(); 导致另一个事件。当执行相应的事件处理程序时,您再次直接或间接调用热键的处理程序。


我认为问题是返回,您可以只删除并再次检查吗?

i don't know why its happening

i wrote a code to toggle play pause for vlc using hotkey and on pressing hotkey it repeats itself two time and thus nullify result.

private void hotKeyPlayPause_HotKeyPressed(object sender, SmartHotKey.HotKeyEventArgs e)
        {
            if (videoLoaded)
            {
                if (playing)
                {
                    btnPause.Visible = false;
                    btnPlay.Visible = true;
                    playing = false;
                    axVLCPlugin1.pause();
                    return;
                }
                else
                {
                    btnPause.Visible = true;
                    btnPlay.Visible = false;
                    axVLCPlugin1.play();
                    playing = true;
                    return;
                }
            }
        }



When i try to debug this and create a breakpoint and follow F11 it shows that it first did pause() action up to return in if block and then jump back to check if(VideoLoaded) block... again enter in if/else block for toggle play pause and as usually this time it will execute else block as playing flag is false in second time execution...

any idea about this bug??? am i missing something???

解决方案

You see, your statement is not accurate. You cannot say that your event is executes two times (events do not "execute"). What you see is that your event handler method is called two times, but this is nothing but a method, which can be called by different reasons.

First of all, check where the event handler is added to an invocation list of event instance, that is, all '+=' operators. Put a breakpoint on the first statement of the handler and run it under the debugger. When execution comes to the breakpoint, see the "Call Stack" window (under the "Debug" menu). It will show you where the call comes from in both cases.

—SA


Be careful on which events you react: sometimes reacting to other events while reacting to an original event may cause confusion.
I think that axVLCPlugin1.pause(); causes another event. When the corresponding event handler is executed, you call again the handler for your hotkey, directly or indirectly.


i think the problem is with return can you just remove and check again?


这篇关于单个事件不必要地执行两次!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆