任何人都建议不能停止计时器控制 [英] not able to stop timer control can any one suggest

查看:89
本文介绍了任何人都建议不能停止计时器控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

i have made a timer control in main()
{
                  System.Timers.Timer aTimer = new System.Timers.Timer();
                aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
                aTimer.Start();
                aTimer.Interval = 1000;
                aTimer.Enabled = true;
}



并将其称为


and call it

private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
            try
            {
                foreach (System.Int32 i in Enum.GetValues(typeof(Keys)))
                {
                    if (GetAsyncKeyState(i) == -32767)
                    {
                        if (Keys.Control == Control.ModifierKeys) Form1.crtl = 1;
                        if (Keys.K == (Keys)i) Form1.k = 1;
                    }
                    if (Form1.k == 1 && Form1.crtl == 1)
                    {
                        string response = ShowDialog("Password Required: ", "Password");
                        
                        if (response.Equals("###########"))
                        {

                            Form1 frm = new Form1();
                            frm.Show();
                            Form1.k = 0;
                            Form1.crtl = 0;
                        }
                    }
                    else
                    {

                        Form1.k = 0;
                        Form1.crtl = 0;
                    }
                }
            }
            catch (Exception)
            {
            }
            finally
            {

            }
}





但是在这种情况下我的显示对话框形式是开放的,但一次又一次可以帮助这个



我的简单任务是在演出之后对话我的计时器将停止



but in this case my show dialog form is open but again and again can any one help for this

my simple task is after show Dialog my timer will be stop

推荐答案

你怎么能抱怨没有停止计时器?在您的代码中,我找不到对 Stop 的调用。使用它:

https://msdn.microsoft.com/en-us/library/system.timers.timer.stop%28v=vs.110%29.aspx [ ^ ]。



现在,您无法直接在计时器事件处理程序中使用UI,因为无法保证在UI线程中调用它。您不能从非UI线程调用与UI相关的任何内容。相反,您需要使用 Invoke System.Windows.Threading的方法。 Dispatcher (对于Forms或WPF)或 System.Windows.Forms.Control (仅限表单)。



您将在我过去的答案中找到有关其工作原理和代码示例的详细说明:

Control.Invoke()与Control.BeginInvoke() [ ^ ],

使用Treeview扫描仪和MD5的问题 [ ^ ]。



另请参阅有关线程的更多参考资料:

如何让keydown事件在不同的操作上运行vb.net中的线程 [ ^ ],

在启用禁用+多线程后控制事件未触发 [ ^ ]。







有一种选择:使用非常非常差的计时器类, System.Windows.Forms .Timer

https://msdn.microsoft.com/en-us/library/system.windows.forms .timer%28v = vs.110%29.aspx [ ^ ]。



为什么?因为它可以在没有调用的情况下使用,因为事件都是在UI线程中处理的。它不仅会导致非常糟糕的准确性,而且时间准确性可能只是毁灭性的。忘记任何周期性的事情:甚至不要接近。但是在一些罕见的情况下(通常,当你只想处理一次事件时),当你根本没有时间准确性的要求时,它会很好。如果是这种情况,请执行此操作。



-SA
How can you complain about not stopping the timer? In your code, I fail to find the call to Stop. Use it:
https://msdn.microsoft.com/en-us/library/system.timers.timer.stop%28v=vs.110%29.aspx[^].

Now, you cannot use the UI directly in the timer event handler, because it is not be guaranteed to be called in the UI thread. You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].



There is one alternative: using a timer class of very, very poor quality, System.Windows.Forms.Timer:
https://msdn.microsoft.com/en-us/library/system.windows.forms.timer%28v=vs.110%29.aspx[^].

Why? Because it can be used without invocation, because the events are all handled in the UI thread. Not only it leads to very bad accuracy, but time accuracy can be just devastating. Forget about anything periodic: not even close. But it can be good in some rare cases (usually, when you want to handle the event only once), when you have no requirements to time accuracy at all. If this is your case, do it.

—SA


https://msdn.microsoft.com/en-us/library/ system.timers.timer.autoreset(v = vs.110).aspx [ ^ ]



取决于需要但是计时器可以在此时重新启用或停止(在处理程序中)。



见这里:System.Timers.Timer单线程使用情况 [ ^ ]


这篇关于任何人都建议不能停止计时器控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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