Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 4-Oct-11 4:52am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

System.Timers.Timer does not have a Tick but has a Elapsed event.
 
You really should use intellisense more.
  Permalink  
Comments
USABebopKid at 4-Oct-11 10:59am
   
"You really should use intellisense more."
 
Amen!
Mehdi Gholam at 4-Oct-11 11:02am
   
Hallelujah brother!
SAKryukov at 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 at 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 at 4-Oct-11 12:53pm
   
At times like these you need a break, you need a K..K..!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

Here is the exact code:
 
//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:
 
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
  Permalink  
v4
Comments
Mehdi Gholam at 4-Oct-11 12:42pm
   
The master at work! 5!
SAKryukov at 4-Oct-11 12:42pm
   
Wow! Sounds flattering. Thank you, Mehdi.
--SA
Simon Bang Terkildsen at 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
SAKryukov at 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
Simon Bang Terkildsen at 4-Oct-11 12:52pm
   
My 5 for showing how to actually use System.Timers.Timer
SAKryukov at 4-Oct-11 13:06pm
   
Thank you, Simon.
--SA
TimGMitch at 4-Oct-11 14:31pm
   
SA - Thankyou for your brave attemps to help this C# rookie. I have attempted to implement your code suggestion and my code now looks as follows:
 
Timer autoCloseTimer = new Timer();
autoCloseTimer.Interval = 3000;
autoCloseTimer.Enabled = true;
autoCloseTimer.Elapsed += (sender, eventArgs) =>
{
this.Invoke(new System.Action((FakeMessageBox) => { FakeMessageBox.Close(); }), box);
};
 
I now have errors with "sender" and the "this.Invoke" statement.
phil.o at 13-Oct-11 10:44am
   
Close method of FakeMessageBox class surely isn't a static one.
So you should stick to the syntax that has been given ; i.e. :this.Invoke(new System.Action\((fmb) => {fmb.Close();}), box);
SAKryukov at 13-Oct-11 15:01pm
   
Wrong. The method can be instance (non-static). Action should be a delegate: where it is declared? Or it should be Action. Where is it?
--SA
SAKryukov at 13-Oct-11 15:02pm
   
Incorrect. Please see my comment below and read my code sample more thoroughly. In non-generic syntax I declared Action above. Did you?
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

System.Timers.Timer doesn't have a Tick property.
 
But System.Windows.Forms.Timer does.
 
You are using the wrong timer.
 
Smile | :)
  Permalink  
Comments
Mehdi Gholam at 4-Oct-11 10:58am
   
Just posted the same, my 5!
SAKryukov at 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 at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

System.Timers.Timer does not have a Tick events...
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

Hi,
I think first u should start the timer by using start method....
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

I'm guessing you switched from a different type of Timer... That particular one calls the event you're looking for "Elapsed"
  Permalink  

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



Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 13 Oct 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100