5

I have a windows application and I want any event when user changes the time of that system on which that windows application is opened or running.

How can I get that changed time or specific time or any event of system change of time/date?

Damon
  • 3,004
  • 7
  • 24
  • 28
k-s
  • 2,192
  • 11
  • 39
  • 73
  • 1
    Possible duplicate of [Subscribe to event of changing time on local system](http://stackoverflow.com/questions/3787299/subscribe-to-event-of-changing-time-on-local-system) – user May 22 '16 at 21:23

2 Answers2

8

You can subscribe to SystemEvents.TimeChanged event.

To subscribe to above event, do the following:

1.Create TimeChanged EventHander.

    private void time_Changed(object sender, EventArgs e)
    {
       MessageBox.Show("Time Changed");  
    }

2.Add above event handler to SystemEvents.TimeChanged event.

 SystemEvents.TimeChanged += time_Changed;
Akash KC
  • 16,057
  • 6
  • 39
  • 59
  • How can I use that method? I am bit confused here as am new to windows app. – k-s Aug 14 '13 at 12:03
  • Yes, but make sure there is a message pump running. This won't work in a console application unless you create a message pump. See http://stackoverflow.com/a/764904/56778 for an example. – Jim Mischel Aug 14 '13 at 12:05
  • I wrote my code, but message box is not firing on change of system time... See below code. public partial class Form1 : Form { public static event EventHandler TimeChanged; public Form1() { InitializeComponent(); TimeChanged += Form1_TimeChanged; } protected void Form1_TimeChanged(object sender, EventArgs e) { MessageBox.Show(DateTime.Now.ToString()); } } – k-s Aug 14 '13 at 12:06
0

...or any event of system change of time/date? SystemEvents.TimeChanged will only trigger "when the user changes the time on the system clock".

System time changes may also occur due to system time adjustments, being initiated by the system or the user. You may use the GetSystemTimeAdjustment function to capture such system time adjustments.

Arno
  • 4,994
  • 3
  • 39
  • 63