473,396 Members | 1,599 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,396 software developers and data experts.

Help:windows Service and Timer

I have a Windows Service project in C#.net and a timer during which a
certain function must be called.
Timer interval is say 3 minutes.
The function in the timer_Elapsed event.

Problem is when i start the service, the timer is directly firing. I
want the timer event to be fired after 3 minutes of starting the
service. how do i do that?

*** Sent via Developersdex http://www.developersdex.com ***
Dec 7 '05 #1
6 2979
Although there might be some other ways to do this, the easiest way is
declaring a bool flag to determine if this is the first time the event
is called.

private bool isFirstCall = true;

In timer_Elapsed:
if (isFirstCall)
{
isFirstCall = false;
return;
}

// Do you stuff here

Hope it helps,
Thi

Dec 7 '05 #2

I have another problem: i want the service to start at a specific time
for example @4:00 AM. I cannot set it to 24 hours cause what if the
service stopped and started again in the same day.
How is that?
*** Sent via Developersdex http://www.developersdex.com ***
Dec 7 '05 #3
Hi,

You can create a scheduled job to start the service

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Marie-Christine Bechara" <ma*********************@ifsal.com> wrote in
message news:%2******************@TK2MSFTNGP14.phx.gbl...

I have another problem: i want the service to start at a specific time
for example @4:00 AM. I cannot set it to 24 hours cause what if the
service stopped and started again in the same day.
How is that?
*** Sent via Developersdex http://www.developersdex.com ***

Dec 7 '05 #4


By service I mean Windows service in C#.net.
If time interval is 24hours (starting from 4:00 AM) and
windows service stopped @ 6:00 AM, then if i restart it @6:00AM, instead
of entering the time event next time it is 4:00 AM, it will enter it
next time @6:00AM.

I don't want that. how can i resolve this issue?

*** Sent via Developersdex http://www.developersdex.com ***
Dec 7 '05 #5
In your windows service you can still view and access the system clock.
Have your service fire off it's system timer every x number of seconds
and then check the system clock. If it's > 4am and < 10 am then do
your work, if doing your work after 10am then stop doing your work.

Regardless of when your service is stopped / started you can use the
system clock to determine when to run this way your service can run
24/7.

The only drawback to .net services is the rather nasty amount of memory
they consume to do relatively minute tasks.

Dec 7 '05 #6
If I understand it correctly you want an method to be invoked at a
specific time (4 am) daily. If your need is more complex, you might
need to write a scheduler which constantly check system time on a
background thread and invoke registered callbacks. In your case, you
could use the same approach in my previous post:
- When init the timer, set it interval to the timespan from now to next
4pm
- On the first ellapsed event invocation, reset the interval to 24h

In your init routine:
DateTime now = DateTime.Now;
DateTime today4PM = new DateTime(now.Year, now.Month, now.Day);
today4PM.Hour = 4;
DateTime next4PM = today4PM;
if (now > today4PM) next4PM = next4PM.AddDays(1);
Timer theTimer = new Timer();
theTimer.Interval = (next4PM - now).Millisecond;
theTimer.Start();

In timer_Elapsed:
if (isFirstCall)
{
isFirstCall = false;
// Reset interval to 24h
theTimer.Interval = 1000*60*60*60*24 // one day
}

// Do your stuff

Regarding your first problem, I have just looked at the documetation of
System.Timers.Timer and it says the event is not fired immediately. The
first time the event is invoked is when the Interval ellapsed. Besure
you set Interval *before* calling Start (or set Enabled to true)
because the default Interval is 100 milliseconds.

Thi

Dec 8 '05 #7

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

Similar topics

3
by: dpackwood | last post by:
Hello, I have two different scripts that do pretty much the same thing. The main perl script is on Windows. It runs and in the middle of it, it then calls out another perl script that then...
2
by: Bob Scott | last post by:
Hi All Can anyone help. I have a site thats been up for 2-3 years on an NT4 server using IIS & Ms Access, and I used MAcromedia Ultradev to build it with MX 2004 to maintain it. The hosting...
8
by: Bob | last post by:
This is a bug that affects all controls, not just the ones that it's screwing up in my app. I really need a workaround. Anyone? Please? Before I regret letting my MSDN subscription run out and pay...
1
by: Kurt Skaronea | last post by:
Not sure if this is the right spot for this, but I couldn't find any other logical place. I have a service that installs and runs fine. In the properties dialog of the service, on the General...
1
by: David A. Osborn | last post by:
Can someone point me in the right direction for making standard windows help windows for my application?
5
by: quortex | last post by:
Hi all, I have a class which contains a method called GetMessage(). When I compile and unit test it all is well. However in the application I want to use it in there appears to be a conflict...
0
by: =?Utf-8?B?c2F2?= | last post by:
hi i hava a navman pin 570 which was running windows mobile 3... i have a lot of contacts that i kept backed up on the sd card... the device is no longer working and the backup file which is an...
7
by: ssecorp | last post by:
I am not clear about the results here. from timeit import Timer import Decorators def fib(n): a, b = 1, 0 while n: a, b, n = b, a+b, n-1
2
by: dnuthtsaso | last post by:
Where do i put my code in this -------------------------- #include <windows.h> /* Declare Windows procedure */ LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM); /* Make...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.