Reputation: 23
the question is: how can i for example every two seconds without user intervention make a label flashes. like every 2 seconds label.text="". and the next two seconds i do this label1.text="visible" or changing another property like color or anything.
could you help me with a basic code, thank you.
Upvotes: 0
Views: 215
Reputation: 133995
The easiest way to do this is with System.Windows.Forms.Timer. You can drop it on your form and set the interval, and create the Tick
event handler. The linked documentation has a simple example that you can easily modify to work with your label.
The beauty of using this timer is that it automatically synchronizes with the UI thread, so you don't have to worry about Invoke
, BeginInvoke
, etc.
Upvotes: 0
Reputation: 10359
What you could do with one thread is something like this :
In a loop.
With 2 threads, I would recommend something like this : C# Running timed job on StackOverflow
Edit : use QuartzNet (tutorial).
A link on StackOverFlow.
Upvotes: 0
Reputation: 141638
Your question is a bit difficult to understand, but I'll give it my best shot.
From what I understand, you want your code to do something every two seconds. The easiest way to do that is with a timer. The System.Timers.Timer
class is useful for that, as well as System.Threading.Timer
. Which you use is up to you, but the one in the Threading namespace is a little more primitive.
Timers operate on a ThreadPool, so if you will be manipulating a Windows Form, make sure what you do happens on the GUI thread either by using Control.Invoke
for Windows Forms or Dispatcher.Invoke
for WPF. Timers are also a little tricky because their Threading Apartment is typically MTA, so if you try to access the clipboard or something like that, you may get errors.
If you want to ensure your timer fires exactly at a certain time, rather than at a specific interval, you could make a timer with a period that starts another timer, or adjusts itself. I would argue that if you are doing something every 2 or 5 seconds do you really need it to happen at a very specific time? Timers, and Windows Time in general isn't specific enough and has small inaccuracies - it will drift over time by a few milliseconds.
Upvotes: 1
Reputation: 101604
You may try using the timer with a 2-second interval. But keep in mind, whenever you play with threads outside of the UI, you need to look in to delegates and BeginInvoke
To accomplish what you're after, you could run it every 1 second, then check against DateTime.Now to see if the clock matches your criteria.
Upvotes: 1