Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
4.00/5 (5 votes)
See more:
i have a timer in my code,upon completion of that timer i want to raise buttonclick event,which is written some where,how will i do that??


thanks in advance
Posted
Comments
Sunasara Imdadhusen 17-Mar-11 0:54am    
Not clear!!

However my question why you want to raise an event?. Instead you can move the logic to a common function which can be called from the timer and as well as from the button click. Tight coupling is not freedom
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 17-Mar-11 1:26am    
Absolutely not. A 5 and see my answer (in contrast to yours :-)
--SA
Sergey Alexandrovich Kryukov 17-Mar-11 1:34am    
Albin, remember Josh Machol, the guy who voted 1 against John of flicker, and has a long discussion on proper behavior? You had to remove your correct Answer because Josh told us he already did it. Do you remember him?

I made him!

He did not do it properly, confessed after I asked three times. Epic fail due to quite a stupid logic.

Interested to see what was really the problem? See my Answer:
http://www.codeproject.com/Answers/169644/Flickering-Transparency-and-BackgroundImages.aspx

This is really funny!

And I also added to that argument with John, if you want to see at the very end...
--SA
Albin Abel 17-Mar-11 1:39am    
Loking at it
Albin Abel 17-Mar-11 1:46am    
Your profile picture with that eye makes the difference. I voted there, but only 5 is possible in code project
Sergey Alexandrovich Kryukov 17-Mar-11 1:56am    
Thank you so much. What about the profile...
Yes, the eye... As I say: "I watch you!".
--SA
The way you ask it:

C#
public class MyButton : Button {
    internal void DoInvokeOnClick() {
        InvokeOnClick(this, new EventArgs());
    } //DoInvokeOnClick
} //MyButton

//...

MyButton button = new MyButton();
Timer MyTimer //...
//...

Timer.Tick += (sender, eventArfs) => {
   button.DoInvokeOnClick();
}; //Timer.Tick


That was according to your requirements, formally. But you should not place such requirements. Why creating a Button derived class? (This is the only way to expose InvokeOnClick, as it is protected.) The reasonable way is abstracting out the action you do on click. So, not following you requirement literally will be much better:

C#
Button button = new MyButton();
Timer MyTimer //...

//...
void  MyClickAction() { /* do whatever you need */ }

button.Click += (sender, eventArfs) => { MyClickAction(); }

Timer.Tick += (sender, eventArfs) => {
   MyClickAction();
}; //Timer.Tick


This is actually what Albin suggested his Answer when he questioned "why raising event?" Much easier!

Now, a big warning for you. Don't use timer, especially System.Windows.Forms.Timer if you want to stay out of trouble. In worst case, use System.Timers.Timer. Still a lot of trouble. Did you ever plan for situations when a new timer tick comes when you process of the previous tick is not finished? The are sources of time troubles which are hard to detect.

Instead, use thread. To get you an idea, see my collection of past Questions on the topic and discussions: Multple clients from same port Number[^].

—SA
 
Share this answer
 
v3
Comments
Albin Abel 17-Mar-11 1:37am    
:) 5+
Sergey Alexandrovich Kryukov 17-Mar-11 1:49am    
Thank you very much.
--SA
thatraja 17-Mar-11 2:50am    
Fine Answer SA.
Sergey Alexandrovich Kryukov 17-Mar-11 2:51am    
Thank you.
--SA
Espen Harlinn 17-Mar-11 3:37am    
5ed!
Just call the button event on timer_tick

<pre lang="cs">
        private void timer1_Tick(object sender, EventArgs e)
        {
            button1_Click(sender, e);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Text = DateTime.Now.ToString();
        }

 
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