Click here to Skip to main content
15,887,892 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I think I am making this harder than it should be..

What my code does now is that it tries to change the myLabel1 to firstBlinkName; AND secondBlinkName; at the same time.. I would like some time in between, I really dont know how to do this.

I need to create something like this..

C#
            var firstBlinkName = nameBlinkFirstName.Text;
            var secondBlinkName = nameBlinkSecond.Text;

Label.Text = firstBlinkName;
//Pause of 1 second
Label.Text = secondBlinkName;




But this is how it ended up

C#
private void nameBlinkInterval_Tick(object sender, EventArgs e)
{
    var firstBlinkName = nameBlinkFirstName.Text;
    var secondBlinkName = nameBlinkSecond.Text;
    myLabel1.text = firstBlinkName;
    myLabel1.text = secondBlinkName;
}


What I have tried:

Everything I've tried is int he question
Posted
Updated 20-Jun-16 21:13pm

1 solution

I would do it via a separate thread, which is more reliable way. It depends on purpose though. Some inaccuracy in time can be compensated be taking real time reading in each iteration of a loop. It's good, for example, to make impression of smooth motion; in other cases it is not good. You can even create a realistic clock, but you would need a delay (Thread.Sleep) somewhat smaller then the period of time for an update a clock view.

With a timer, you need to be more careful. First idea is: you probably need some near-periodic behavior. If so, make sure you never use the timer type System.Windows.Timer: it is simple and guarantees that the event handler is called in the UI thread, but its timing accuracy is prohibitively bad. It can be used for, say, implementing of timeouts when accuracy is not critical at all.

Let's come back to other timers which can give you reasonable accuracy. They cannot guarantee the call of your handler method in your UI thread. You have to take it into account. You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke(),
Problem with Treeview Scanner And MD5.

See also more references on threading:
.NET event on main thread,
How to get a keydown event to operate on a different thread in vb.net,
Control events not firing after enable disable + multithreading.

—SA
 
Share this answer
 
Comments
BladeLogan 21-Jun-16 4:39am    
Very informitive answer but I went with my own method.. It might not be the best but it works and I'm not having any issues with it what so ever.. This is what I did..

I create this outside of my function, in the class space.

and then this in my tick event..

if(blink == 1){
blink = 2;
label1.text = textbox1.Text;
} else {
blink = 1;
label1.text = textbox2.Text;
}
Sergey Alexandrovich Kryukov 21-Jun-16 9:38am    
This is not "your own method"; this is the same thing. You have to put it under Invoke or BeginInvoke. What is your timer type?
We never discussed the content of the label. It's just irrelevant.
—SA

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