Click here to Skip to main content
15,911,531 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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
C#
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
Posted
Updated 9-Mar-15 20:48pm
v3
Comments
Sergey Alexandrovich Kryukov 10-Mar-15 2:48am    
System.Timers.Timer is not a control.
—SA
Aditya Chauhan 10-Mar-15 2:51am    
then how can make a timer control in c# with console
Sergey Alexandrovich Kryukov 10-Mar-15 2:55am    
Timer as a control? Some do such thing, but this is nonsense, really. Why? You are using proper Timer class already, I appreciate it, but you need to use it correctly.
—SA
Sergey Alexandrovich Kryukov 10-Mar-15 3:07am    
However... see also the update to my solution, after [EDIT]; it could be the alternative you need.
—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[^].

[EDIT]

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
 
Share this answer
 
v4
Comments
[no name] 10-Mar-15 3:14am    
Good answer.
Sergey Alexandrovich Kryukov 10-Mar-15 3:17am    
Thank you.
—SA
https://msdn.microsoft.com/en-us/library/system.timers.timer.autoreset(v=vs.110).aspx[^]

Depends on the need but the timer could either be re-enabled or stopped at this point (in the handler).

See here:System.Timers.Timer "single-threaded" usage[^]
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 10-Mar-15 2:54am    
This is a very good point, but how do you know that the inquirer wanted to use the timer event only once? Just asking; I wasn't able to figure it out.

However, there are other problems I could see; please see Solution 2.

—SA
[no name] 10-Mar-15 3:10am    
I agree completely. Depends on the need but the timer could either be re-enabled or stopped at this point (in the handler).
Sergey Alexandrovich Kryukov 10-Mar-15 3:13am    
I did not mean re-enabled. This is yet another thing, which, as I can see from new inquirer's comment, is his case. I discussed the difference between this and periodic handling, as well as one more alternative I mentioned in my solution after [EDIT].
—SA
Aditya Chauhan 10-Mar-15 3:04am    
no i am not want once
after dialog open i want to stop it when i write a correct password then timer will again start.
Sergey Alexandrovich Kryukov 10-Mar-15 3:09am    
This is the same as once. I meant to say, if you don't want periodic events. If your requirements to time accuracy is pretty low.
Then my alternative, Solution 2, after [EDIT], could be better for you.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900