Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want a situation that when I start my timer, it doesn't tick at its interval first before performing an action. For example, I want to display a label text on my form after I click a button with a 5 second interval but instead after the button is clicked, there is a 5 second delay before the label text is shown. How can I solve this issue?
Code looks something like this:

C#
private void btnSet_Click(object sender, EventArgs e)
{
   timer1.Enabled = true;
   timer1.Start();
   timer1.Interval = 5000;
}

private void timer1_Tick(object sender, EventArgs e)
{
   label1.Text = "Hello";
}
Posted
Updated 13-Oct-15 4:25am
v2
Comments
Leo Chapiro 13-Oct-15 10:07am    
>How can I solve this issue?
You can show us your source code, for example!
Marked as "Incomplete".
Member 11971544 13-Oct-15 10:31am    
updated
[no name] 13-Oct-15 10:18am    
You need to provide complete scenario like:
1. Windows/Web application
2. What is the code you are using(not complete code only which are related to Question)
Member 11971544 13-Oct-15 10:31am    
code provided
Richard Deeming 13-Oct-15 10:39am    
Your code says, "wait five seconds, then set the label's text".

Therefore, it's not surprising that the code waits five seconds before setting the label's text.

If you don't want a five second delay before setting the label's text, then don't use a Timer to wait for five seconds before setting the label's text.

If you do want the delay, then you're going to have to explain what the problem is.

Write your action as a function and call the function before the timer starts

Your code is probably like this

C#
timer.Start();

void timer_click()
{
    // some code
}


change it to

C#
myFunction();
timer.Start();

void timer_click()
{
    myFunction();
}
void myFunction()
{
    // some code
}
 
Share this answer
 
Comments
Member 11971544 14-Oct-15 8:30am    
Same there is still the timer interval occurring before the function
Try with below code:
C#
private void btnSet_Click(object sender, EventArgs e)
{
   timer1.Enabled = true;
   timer1.Interval = 5000;
   timer1.Tick += new EventHandler(timer1_Tick);
   timer1.Start();
   
}
 
private void timer1_Tick(object sender, EventArgs e)
{
   label1.Text = "Hello";
}
 
Share this answer
 
You can do this by declaring an inline delegate like:

C#
private void btnSet_Click(object sender, EventArgs e)
{
   var t = new Timer();
   t.Interval = 5000;
   t.Tick += (s, e) =>
   {
       label1.Text = "Hello";
       t.Stop();
   };
   t.Start();
}


where s, e two arguments are Object sender, EventArgs e.
 
Share this answer
 
Comments
Member 11971544 14-Oct-15 8:27am    
This unfortunately doesn't solve the problem, timer tick still occurring before action

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