0

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.

i give up

could you help me with a basic code, thank you.

user527825
  • 23
  • 1
  • 5
  • I don't understand your question. http://tinyurl.com/so-hints – Oded Dec 27 '10 at 15:35
  • When you mention time, do you mean elapsed time since you started the loop OR the actual system time? If you looking for elapsed time, you could take a look at the StopWatch class. – Waleed Al-Balooshi Dec 27 '10 at 16:02

4 Answers4

1

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.

Brad Christie
  • 100,477
  • 16
  • 156
  • 200
1

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.

vcsjones
  • 138,677
  • 31
  • 291
  • 286
0

What you could do with one thread is something like this :

  • Sleep 2 seconds
  • DoAction
  • Sleep 2 seconds
  • DoAnotherAction

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.

Community
  • 1
  • 1
LaGrandMere
  • 10,265
  • 1
  • 33
  • 41
  • how can i determine the time? – user527825 Dec 27 '10 at 15:39
  • @user527825 : Use another Tick Thread, look at the article I set in my edit :) – LaGrandMere Dec 27 '10 at 15:41
  • Why would you use a thread to do this when it's so much easier to just use a timer? This is a completely inappropriate use of threads. – Jim Mischel Dec 27 '10 at 17:35
  • @Jim Mischel : Simply because I didn't know about the Timer class, and while I reckon it's a lot simpler with it, if you don't know about it and you need to have exactly 2 seconds between each call, you'll end up using 2 Threads, 1 Tick Thread (the main Thread) and 1 or more Working Threads. The fact is that, even if I use the Timer, I would use Working Threads to do the job each 2 seconds in the OnTimed Event. – LaGrandMere Dec 27 '10 at 17:47
0

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.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351