Re: System.Timer!

Tech-Archive recommends: Fix windows errors by optimizing your registry



"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.



.



Relevant Pages

  • [PATCH 2/2] Char: timers cleanup
    ... Use timer macros to set function and data members and to modify ... static int dtlk_port_lpc; ... static void dtlk_handle_error; ... static char wdt_expect_close; ...
    (Linux-Kernel)
  • [PATCH 2/2 v4] SGI RTC: add clocksource driver
    ... This patch provides a driver for SGI RTC clocks and timers. ... This provides a high resolution clock and timer source using the SGI ...
    (Linux-Kernel)
  • [PATCH 2/2 v5] SGI RTC: add clocksource driver
    ... This patch provides a driver for SGI RTC clocks and timers. ... This provides a high resolution clock and timer source using the SGI ...
    (Linux-Kernel)
  • [GIT PULL] timer updates for v2.6.30
    ... NTP code cleanups and fixlets. ... The ICH10 force-enable hpet change brings regression risks on ... timer 0 and timer 1 in case of RTC emulation. ... +static void hpet_start_counter ...
    (Linux-Kernel)
  • [PATCH 1/2 v3] SGI RTC: add clocksource driver
    ... This patch provides a driver for SGI RTC clocks and timers. ... This provides a high resolution clock and timer source using the SGI ...
    (Linux-Kernel)