473,796 Members | 2,536 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.T imer _timer = new System.Timers.T imer();

protected override void OnStart(string[] args)
{
base.OnStart(ar gs);
_timer.Interval = 1000;
_timer.Elapsed += new ElapsedEventHan dler(timeElapse d);
_timer.Start();
}

void timeElapsed(obj ect sender, ElapsedEventArg s args)
{
if (DateTime.Compa re(runAt, DateTime.Now) ==
0)
{
// Spawn a new thread to do the cleanup
work
WorkerThread threadObj = new WorkerThread();
Thread workerThread = new Thread(new
ThreadStart(thr eadObj.BeginTas k));
workerThread.St art();
}
}

The problem is that the DateTime.Compar e 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 3115
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...@g mail.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
4713
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 log-entries I have created on stop() and start() in the eventlog works fine as well. However - the actual task is never executed. I catch the execution of the tick and in the finally-block I write to the eventlog as well. But only one entry is creates...
3
2199
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 issue is simply getting timers to work. I have a DatabaseManager.DataManagerFacade which contains a timer. Every 6 seconds it updates stock data using an online webservice. I wrote have 2 possible startup projects to make the service work. One...
3
350
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 when a service is stopped or started. What i'm trying do do now is adding a timer so after a specific period of time the service does something, in the example below writing to a text
5
4273
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 connects to the Server, the server will process and send result to the client.(This is Client Server Application. The Server side Code is implementd in th OnStart method Of Windows Service). When i tried the Client Server Application in Console...
7
1811
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 so it's always running. Is there some kind of timer event I can use? Thanks, Doug
3
5875
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: public partial class CheckAccountsService : ServiceBase {
2
4957
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 method may succeed one time and not another. I cannot find any reason why it would work one time and not another. The exception occurs every two or three calls to the web method. The service utilizes a COM component provided by PeopleSoft...
2
1770
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 they both inherit ServiceBase. The main Method is as follows: static void Main() { System.ServiceProcess.ServiceBase ServicesToRun; ServicesToRun = new System.ServiceProcess.ServiceBase
0
1507
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 every <xseconds. I'd like to be able to somehow get the current 'elapsed' seconds of the timer, with a view to displaying something on the service control manager program I have written. All seems to be OK (service works properly, as does the DLL and...
0
9680
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9528
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10456
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10174
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10012
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7548
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6788
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5442
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5575
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.