System.Timer!

  • Thread starter Thread starter Vai2000
  • Start date Start date
V

Vai2000

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
 
Don't use a windows service. Instead, switch it to a console or
windows application, and setup a Scheduled task.
 
Vai2000,

I would to this. I would set up first a small timer (meaning a small
interval) and then at this timer activation, I would check the datetime.now
and by then set up another time with a bigger interval that would reach the
desired time you want. Using this approach, even that the service is stopped
and then re-started again, it would correct the timer to fire at the right
time.
 
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.
 
Great, will take WMI Solution!, thanks ya all

Willy Denoyette said:
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.
 
Vai2000 escribió:
Great, will take WMI Solution!, thanks ya all

But take in account that WMI is not portable.

Regards,

Andrés [ knocte ]

--
 
"Andrés G. Aragoneses [ knocte ]" said:
Vai2000 escribió:
Great, will take WMI Solution!, thanks ya all

But take in account that WMI is not portable.

?? Who's talking about portability here? The OP is talking about .NET Framework based
Windows Services, are these considered portable?

Willy.
 
Willy Denoyette [MVP] escribió:
"Andrés G. Aragoneses [ knocte ]" said:
Vai2000 escribió:
Great, will take WMI Solution!, thanks ya all

But take in account that WMI is not portable.

?? Who's talking about portability here? The OP is talking about .NET
Framework based Windows Services, are these considered portable?

Yep, you can launch windows services with Mono.

Regards,

Andrés [ knocte ]

--
 
"Andrés G. Aragoneses [ knocte ]" said:
Willy Denoyette [MVP] escribió:
"Andrés G. Aragoneses [ knocte ]" said:
Vai2000 escribió:
Great, will take WMI Solution!, thanks ya all

But take in account that WMI is not portable.

?? Who's talking about portability here? The OP is talking about .NET
Framework based Windows Services, are these considered portable?

Yep, you can launch windows services with Mono.

Regards,

Andrés [ knocte ]

--

Yep, albeit not documented yet (just like a bunch of other stuff), Anyway, who asked about
mono?
Even when "Services" are in the mono distro what makes you wonder that the Services YOU
implement on windows are/should be portable? Services are always for specialized/dedicated
tasks. This is not about portability, this is about availability. All depends on what your
service's task looks like. Say that I'm accessing an SQL Server Analysis Server or BizTalk,
or simply,that I'm using V3's WCF from a service, should I stay away from it because - not
available - on other platforms?

Willy.
 
Willy Denoyette [MVP] escribió:
Yep, albeit not documented yet (just like a bunch of other stuff),

Not documented in Microsoft documentation, but...

Anyway, who asked about mono?

Nobody. But I thought that for the 90% people of this list,
interoperability and portability are important. And now, the most
interesting platform that allows this with .NET is Mono.

Even when "Services" are in the mono distro what makes you wonder that
the Services YOU implement on windows are/should be portable? Services
are always for specialized/dedicated tasks. This is not about
portability, this is about availability. All depends on what your
service's task looks like. Say that I'm accessing an SQL Server Analysis
Server or BizTalk, or simply,that I'm using V3's WCF from a service,
should I stay away from it because - not available - on other platforms?

Well, that's your decision. In my case, my professional circumstances
require me to always look at portability. In the case we are discussing
now, it's just "Vai2000"'s decision. I only just wanted to point out
this to him.

Regards,

Andrés [ knocte ]

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top