Vai2000 said:
Hi All, I have a Win Svc (.net) running on my server. At midnight I need to
do some manipulations....how can I invoke a timer at midnight?
Right now I have plugged in the ElapsedEvent which is being called every 30
seconds inside it I check for datetime whether its midnight or not...
Kindly throw some smart suggestions!!!
TIA
One option is to use WMI and System.Management.
Here is a small console sample...
using System;
using System.Management;
class Program {
public static void Main() {
// Bind to local machine
WqlEventQuery q = new WqlEventQuery();
q.EventClassName = "__InstanceModificationEvent ";
// fire event at 0h0m0s
q.Condition = @"TargetInstance ISA 'Win32_LocalTime' AND TargetInstance.Hour = 0 AND
TargetInstance.Minute = 0 AND TargetInstance.Second = 0";
Console.WriteLine(q.QueryString);
using (ManagementEventWatcher w = new ManagementEventWatcher(q))
{
w.EventArrived += new EventArrivedEventHandler(TimeEventArrived);
w.Start();
Console.ReadLine(); // Block this thread for test purposes only....
w.Stop();
}
}
static void TimeEventArrived(object sender, EventArrivedEventArgs e) {
Console.WriteLine("This is your wake-up call");
Console.WriteLine("{0}", new
DateTime((long)(ulong)e.NewEvent.Properties["TIME_CREATED"].Value));
}
}
Note that the TimeEventArrived will run on a threadpool thread. Your service should
initialize this at the start, your OnStop should call ManagementEventWatcher .Stop.
Willy.