Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

System.Timer!

9 views
Skip to first unread message

Vai2000

unread,
Jan 18, 2007, 12:15:03 PM1/18/07
to
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


Andy

unread,
Jan 18, 2007, 12:19:20 PM1/18/07
to
Don't use a windows service. Instead, switch it to a console or
windows application, and setup a Scheduled task.

Vai2000

unread,
Jan 18, 2007, 12:33:58 PM1/18/07
to
Can't do console, has to be a svc since its polling on a dir

"Andy" <an...@med-associates.com> wrote in message
news:1169140759....@l53g2000cwa.googlegroups.com...

Robson Siqueira

unread,
Jan 18, 2007, 12:50:54 PM1/18/07
to
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.


--
Regards,
Robson Siqueira
Enterprise Architect
"Vai2000" <nos...@microsoft.com> wrote in message
news:eqVYTQyO...@TK2MSFTNGP04.phx.gbl...

Willy Denoyette [MVP]

unread,
Jan 18, 2007, 1:25:19 PM1/18/07
to
"Vai2000" <nos...@microsoft.com> wrote in message
news:eqVYTQyO...@TK2MSFTNGP04.phx.gbl...

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

unread,
Jan 18, 2007, 1:42:44 PM1/18/07
to
Great, will take WMI Solution!, thanks ya all

"Willy Denoyette [MVP]" <willy.d...@telenet.be> wrote in message
news:ORLXE4y...@TK2MSFTNGP03.phx.gbl...

"Andrés G. Aragoneses [ knocte ]"

unread,
Jan 18, 2007, 2:35:04 PM1/18/07
to
Vai2000 escribió:
> Great, will take WMI Solution!, thanks ya all

But take in account that WMI is not portable.

Regards,

Andrés [ knocte ]

--

Willy Denoyette [MVP]

unread,
Jan 18, 2007, 2:44:07 PM1/18/07
to
""Andrés G. Aragoneses [ knocte ]"" <kno...@NO-SPAM-PLEASE-gmail.com> wrote in message
news:%232aAYfz...@TK2MSFTNGP06.phx.gbl...

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

"Andrés G. Aragoneses [ knocte ]"

unread,
Jan 18, 2007, 4:44:21 PM1/18/07
to
Willy Denoyette [MVP] escribió:

Yep, you can launch windows services with Mono.

Regards,

Andrés [ knocte ]

--

sloan

unread,
Jan 18, 2007, 5:29:51 PM1/18/07
to
See this post:
http://groups.google.com/group/microsoft.public.msmq.programming/browse_thread/thread/c927d4bf09417b6b/f07ecfd06fa63c7f?lnk=st&q=lngPeriod+lngDueTime&rnum=1&hl=en#f07ecfd06fa63c7f

You can "code up" a timer. and do a little math to figure out how to get it
pop at an exact time.


See
http://msdn2.microsoft.com/en-us/library/system.threading.timercallback.aspx


"Vai2000" <nos...@microsoft.com> wrote in message
news:eqVYTQyO...@TK2MSFTNGP04.phx.gbl...

Willy Denoyette [MVP]

unread,
Jan 18, 2007, 7:38:42 PM1/18/07
to
""Andrés G. Aragoneses [ knocte ]"" <kno...@NO-SPAM-PLEASE-gmail.com> wrote in message
news:%23MVZnn0...@TK2MSFTNGP03.phx.gbl...

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.

"Andrés G. Aragoneses [ knocte ]"

unread,
Jan 19, 2007, 2:41:02 AM1/19/07
to
Willy Denoyette [MVP] 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.
> 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 ]

--

0 new messages