Click here to Skip to main content
15,887,434 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
With the code below I get an error message saying "System.Timers.Timer does not contain a definition for "Tick" ..."

What am I missing?



using System;
using System.Timers;
namespace AutoCloseMessageBox1
{
    public partial class Form1 : Form
    {
        private FakeMessageBox box;
        private void Form1_Load( object sender, EventArgs e )
        {
            box = new FakeMessageBox();
            box.Show();
            Timer autoCloseTimer = new Timer();
            autoCloseTimer.Interval = 3000;
            autoCloseTimer.Enabled = true;
            autoCloseTimer.Tick += new EventHandler(autoCloseTimer_Tick);
        }
        void autoCloseTimer_Tick(object sender, EventArgs e)
        {
            box.Close();
        }
    }
}
Posted
Updated 13-Dec-16 15:56pm

System.Timers.Timer does not have a Tick but has a Elapsed event.

You really should use intellisense more.
 
Share this answer
 
Comments
ARBebopKid 4-Oct-11 10:59am    
"You really should use intellisense more."

Amen!
Mehdi Gholam 4-Oct-11 11:02am    
Hallelujah brother!
Sergey Alexandrovich Kryukov 4-Oct-11 12:40pm    
Correct, a 5, but what to do? Not so easy. Please see complete code in my solution.
--SA
Simon Bang Terkildsen 4-Oct-11 12:45pm    
My 5.
You really should use intellisense more.
very true, though sometimes it can feel a bit more like stupidsense/dumbsense/What-the-f***-Were-the-Devs-Thinking-Sense. That probably sounds way to harsh and I may soon forgive and forget, I've just had such a bad experience with it today. It started out taking forever to display, like 5-10 seconds!! And then it started showing the intellisense for the wrong types (still taking for ever)!! and then suddenly VS just crashed each time the popup opened.. reintall VS ;( . The only work I got done today was a small bug fix:
if(!pointer) pointer->DoSomthing();
All I got done today was to remove a f***ing !, grr.
Mehdi Gholam 4-Oct-11 12:53pm    
At times like these you need a break, you need a K..K..!
Here is the exact code:

C#
//replace your line with "Tick +=" with:
autoCloseTimer.Elapsed += (sender, eventArgs) => {
    this.Invoke(new System.Action<System.Windows.Forms.Form>((param) => { param.Close(); }), box);
};


The call to Invoke should be done on any Control actually working in your UI; in your case this is your form referenced via the parameter "this".

If by some reason you use C# v.2, you should add declaration of Action and replace lambda syntax with anonymous delegate syntax without type inference:

C#
delegate void Action(System.Windows.Forms.Form box);
void CloseBox(System.Windows.Forms.Form box) { box.Close();}

//...

autoCloseTimer.Elapsed += delegate(object sender, System.Timers.ElapsedEventArgs eventArgs) {
    this.Invoke(new Action(CloseBox), box);
};


I assumed box is a form, too. If not, just replace its type to the right one in Action and CloseBox.

—SA
 
Share this answer
 
v4
Comments
Mehdi Gholam 4-Oct-11 12:42pm    
The master at work! 5!
Sergey Alexandrovich Kryukov 4-Oct-11 12:42pm    
Wow! Sounds flattering. Thank you, Mehdi.
--SA
Simon Bang Terkildsen 4-Oct-11 12:54pm    
Yeah don't boost SA's confidence to much :P. I'm still keeping my eye out for an answer I can down vote :P Though been active here for 2 months and still haven't found one XD
Sergey Alexandrovich Kryukov 4-Oct-11 13:20pm    
Thank you very much for this note, Simon.

I hope high votes cannot affect my confidence, but I would be the most grateful if you catch me on a mistake or offer any other kind of valid criticism (not necessarily constructive). One of such things happened recently; that was my mistake repeated in several answers, so I was very happy about getting a change to fix myself.

Criticism is the most valuable input and driving force in our work. I'm sorry that I can see so many people here who take criticism as offense. I have very little hope in such people and cannot understand how can they grow their mastery with such attitude.

--SA
navyjax2 14-Dec-16 11:06am    
I rated this a "1" since it severely over-complicates things. You don't need delegates and invoke statements, and the Invoke syntax, with its "new Action" this, and its " => { }" that is very complicated and abstract. It is not needed for such a simple action that can be called directly, as the original poster was doing. And none of that fancy stuff had a bit to do with Timers, except for showing how you can add a delegate as a Timer event handler - which might've been too advanced considering it's not the norm. Normally we just call a function and have it do the work. Not showing the normal way things are done makes the beginner's job harder, since then all the guys reading this post begin using delegates, while all the guys out of college are using ElapsedEventHandler and don't have a clue what's going on...
System.Timers.Timer doesn't have a Tick property.

But System.Windows.Forms.Timer does.

You are using the wrong timer.

:)
 
Share this answer
 
Comments
Mehdi Gholam 4-Oct-11 10:58am    
Just posted the same, my 5!
Sergey Alexandrovich Kryukov 4-Oct-11 12:39pm    
No, most usually System.Windows.Forms.Timer is wrong timer (simpler but very bad accuracy), so the method is wrong.
Please see my solution.
--SA
Simon Bang Terkildsen 4-Oct-11 12:49pm    
my 5, Though I agree with SA that you would mostly want to use System.Timers.Timer. But I guess you mean wrong in the sense that the OP thought he was using System.Windows.Forms.Timer
I'm guessing you switched from a different type of Timer... That particular one calls the event you're looking for "Elapsed"
 
Share this answer
 
Just change all the Ticks to Elapsed and add a .Start():

C#
using System;
using System.Timers;
namespace AutoCloseMessageBox1
{
    public partial class Form1 : Form
    {
        private FakeMessageBox box;
        private void Form1_Load( object sender, EventArgs e )
        {
            box = new FakeMessageBox();
            box.Show();
            Timer autoCloseTimer = new Timer();
            autoCloseTimer.Interval = 3000;
            autoCloseTimer.Enabled = true;
            autoCloseTimer.Elapsed += new ElapsedEventHandler(autoCloseTimer_Elapsed);

            autoCloseTimer.Start();
        }
        void autoCloseTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            box.Close();
        }
    }
}
 
Share this answer
 
Comments
Patrice T 13-Dec-16 23:29pm    
5 years too late.
Stop posting solutions to 5 years old question that are already answered.
navyjax2 14-Dec-16 10:59am    
Would, if any of the answers actually could help someone. Saying "add .Start()" or "Timer does not have Tick events" doesn't answer the question. If I were coming on Timers for the first time, I wouldn't know what the hell was going on without an example, and since Google led me here, it seemed an example was a better way to convey what was needed than a bunch of "do this" or "don't do that" statements that these questions seem to get by lazy answerers. And one of the top-rated answers (#6), with its delegates and Invoke statements, severely overly complicates this question.
System.Timers.Timer does not have a Tick events...
 
Share this answer
 
Hi,
I think first u should start the timer by using start method....
 
Share this answer
 

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