473,387 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Windows Service and DateTime issue

Hopefully a simple DateTime problem.

I've got a windows service created in C# on .Net Framework 2.0, the
service uses a system timer to fire an event every second and compare
that against a known time to fire off a worker thread. All the
DateTime objects are using local time as the services are local to the
server.

DateTime runAt = DateTime.Parse("4/24/2007 2:30:00 PM");
System.Timers.Timer _timer = new System.Timers.Timer();

protected override void OnStart(string[] args)
{
base.OnStart(args);
_timer.Interval = 1000;
_timer.Elapsed += new ElapsedEventHandler(timeElapsed);
_timer.Start();
}

void timeElapsed(object sender, ElapsedEventArgs args)
{
if (DateTime.Compare(runAt, DateTime.Now) ==
0)
{
// Spawn a new thread to do the cleanup
work
WorkerThread threadObj = new WorkerThread();
Thread workerThread = new Thread(new
ThreadStart(threadObj.BeginTask));
workerThread.Start();
}
}

The problem is that the DateTime.Compare never says that the two
DateTime objects match so the worker thread isn't created. I've
confirmed that the formats of the two datetime formats are the same
using an Event Log entry and they match perfectly.

Any ideas to why this happening are greatly appreciated.

Apr 24 '07 #1
2 3071
I've
confirmed that the formats of the two datetime formats are the same
using an Event Log entry and they match perfectly.
OK; DateTime doesn't *have* a format - it is just an integer
internally. It is only when you make a string out of it that this
kicks in. I would be *astonished* if 2 DateTimes ever matched in this
scenario; they will always be off by (at least) a few ms.

Presumably you would reset runAt after running (moving it forward)? In
which case the simplest solution is to use "runAfter" - and just run
it the first time you go past the limit. Additionally, note that
Elapsed may be firing on a different thread already, so you might want
to watch out for thread-safety, especially regarding the "runAt" /
"runAfter" variable (only an issue if you also change this var from
another thread).

Marc

Apr 24 '07 #2
On Apr 24, 2:11 pm, Marc Gravell <marc.grav...@gmail.comwrote:
I've
confirmed that the formats of the two datetime formats are the same
using an Event Log entry and they match perfectly.

OK; DateTime doesn't *have* a format - it is just an integer
internally. It is only when you make a string out of it that this
kicks in. I would be *astonished* if 2 DateTimes ever matched in this
scenario; they will always be off by (at least) a few ms.

Presumably you would reset runAt after running (moving it forward)? In
which case the simplest solution is to use "runAfter" - and just run
it the first time you go past the limit. Additionally, note that
Elapsed may be firing on a different thread already, so you might want
to watch out for thread-safety, especially regarding the "runAt" /
"runAfter" variable (only an issue if you also change this var from
another thread).

Marc
Thanks Marc, that makes a lot of sense.

Apr 24 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jesper Stocholm | last post by:
I have created a simple service which just copies a fil to a new file with a new name on certain intervals (the service implements a timer). I have no problems installing the service and the...
3
by: Nathan Kovac | last post by:
I have a feeling I am missing something simple, but I just can't find it. Perhaps someone can give me a lead on where to look. I will describe the issue then post my code to the web service. My...
3
by: Yves Royer | last post by:
Hi all, I have a little question about Windows Services. For my app i need 3 different Windows Services, each with its own functionality. I'm trying to make a test service to see what happens...
5
by: vinoth | last post by:
Hi, I have created WindowsService Project.In that Project OnStart Method i have written the following Code. In this code the Server is waiting for the connection from client. When the Client...
7
by: Doug Stiers | last post by:
I have a VB app that I'm installing as a Windows Service. I want a subroutine in the app to run every 30 minutes during business hours. How do I do this in VB? I set the startup type as automatic...
3
by: sokolo | last post by:
Hello, I wrote a windows service application however it is not working. The service is supposed to enter a new task every 5 sec within Microsoft Outlook. Here is the code for application: ...
2
by: Richard Collette | last post by:
Hi, I have a service, that runs perfectly when executed outside of the web service environment. When called as a web service I get the exception listed below sporadically. A call to the web...
2
by: Steven | last post by:
I have a service process that contains two services but only one of them ever works properly. The background is I have two classes which are Individual() and Mass() in the main service class and...
0
by: JohnP | last post by:
Hi all Does anyone know the best way to get feedback from a Windows Service? I have written a Windows service which uses a class library (a DLL) which has a timer which does some operations...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.