Siddiqui
Siddiqui

Reputation: 7840

How to access the system clock in C#

How to access the system clock in C#? I want to use system clock for time delay purpose instead of using thread.sleep();.

Upvotes: 3

Views: 12354

Answers (3)

ChrisF
ChrisF

Reputation: 137148

DateTime time = DateTime.Now;

will get you the current time.

So if you want to sleep until a specific time you can use the TimeSpan to calculate the number of milliseconds until then. You still will need to use thread.sleep or better still System.Threading.Timer (or System.Timers.Timer) to pause the process.

If, however, you want to start a process at a specific time, you'd be better off creating a separate application and creating a scheduled task to control it. This is accessed via the Control Panel.

Upvotes: 19

JP Alioto
JP Alioto

Reputation: 45117

Your best bet for delay purposes instead of sleep is to use a Timer. You set it for a how often you want it to "tick" and you get events when that happens.

Upvotes: 10

Nathan Koop
Nathan Koop

Reputation: 25197

Are you looking for DateTime struct and the DateTime.Now property?

DateTime.Now;

Upvotes: 4

Related Questions