Re: System.Timer!
- From: "Willy Denoyette [MVP]" <willy.denoyette@xxxxxxxxxx>
- Date: Thu, 18 Jan 2007 19:25:19 +0100
"Vai2000" <nospam@xxxxxxxxxxxxx> wrote in message news:eqVYTQyOHHA.4992@xxxxxxxxxxxxxxxxxxxxxxx
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.
.
- Follow-Ups:
- Re: System.Timer!
- From: Vai2000
- Re: System.Timer!
- References:
- System.Timer!
- From: Vai2000
- System.Timer!
- Prev by Date: Re: A re-announce on GC's defects
- Next by Date: Re: importing csv to datagrid, adding column data
- Previous by thread: Re: System.Timer!
- Next by thread: Re: System.Timer!
- Index(es):
Relevant Pages
|