Click here to Skip to main content
12,747,461 members (37,768 online)
Rate this:
 
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 5:52am
Updated 13-Dec-16 16:56pm
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 4-Oct-11 10:59am
   
"You really should use intellisense more."

Amen!
Mehdi Gholam 4-Oct-11 11:02am
   
Hallelujah brother!
SAKryukov 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..!
navyjax2 14-Dec-16 11:26am
   
@Mehdi and others, And I know I'm late to the party, but I think I need to remind my fellow developers that saying things like "use intellisense more" won't always help someone. I think he saw in there how he couldn't use Tick, or he wouldn't've posted the question. That doesn't mean you automatically, by osmosis or derivation, can somehow automatically know what selection in the intellisense is the actual, correct event. When I saw "Elapsed" the first time, I thought it was a way to set that the Timer had reached its interval, manually, not as a way to make it actually do its work that it would normally do with each interval, like Tick does. Don't assume everyone thinks like or knows as much or can deduce as well as you can. Yes, he could've gone to MSDN to look up what he had, but it doesn't always register that something should be "it" and there are usually a ton of different properties and events to work with, and searching on MSDN could always yield to you the wrong Timer for your project, since they have 3 of them (System.Windows.Forms, System.Threading, System.Timers).
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 4-Oct-11 12:42pm
   
The master at work! 5!
SAKryukov 4-Oct-11 12:42pm
   
Wow! Sounds flattering. Thank you, Mehdi.
--SA
Simon Bang Terkildsen 4-Oct-11 12:52pm
   
My 5 for showing how to actually use System.Timers.Timer
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
SAKryukov 4-Oct-11 13:06pm
   
Thank you, Simon.
--SA
SAKryukov 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
TimGMitch 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<system.windows.forms.form>((FakeMessageBox) => { FakeMessageBox.Close(); }), box);
};

I now have errors with "sender" and the "this.Invoke" statement.
phil.o 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\<FakeMessageBox\>((fmb) => {fmb.Close();}), box);
SAKryukov 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<type>. Where is it?
--SA
SAKryukov 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
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...
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.

:)
  Permalink  
Comments
Mehdi Gholam 4-Oct-11 10:58am
   
Just posted the same, my 5!
SAKryukov 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
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  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 9

Just change all the Ticks to Elapsed and add a .Start():

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();
        }
    }
}
  Permalink  
Comments
ppolymorphe 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.
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  

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

    Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.170215.1 | Last Updated 13 Dec 2016
Copyright © CodeProject, 1999-2017
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