473,396 Members | 2,033 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.

windows service not starting automatically

Hi,

I have tried several ways to make this work but only found work arounds. so I am posting this as a last trial.

I have a windows service which has to kick start and generate a file and email it to our clients every monday and friday.

the service does this only when i stop it and start it. but it has to do this without someone starting it. i know we use a timer for this. i did too. but i dont know whats the issue. the timer is supposed to start everyday and check if its a monday or friday which it is not doing.

below is my code



Expand|Select|Wrap|Line Numbers
  1. namespace export
  2. {
  3.     public partial class ExportService1 : ServiceBase
  4.     {
  5.  
  6.         Timer _timer = new Timer();
  7.         ServiceController controller = new ServiceController();
  8.  
  9.         public ExportService1()
  10.         {
  11.             _timer = new Timer(89000000);
  12.             InitializeComponent();
  13.         }
  14.  
  15.         protected override void OnStart(string[] args)
  16.         {
  17.  
  18.             _timer.Start();
  19.             newfunction(null);
  20.         }
  21.         protected override void OnContinue()
  22.         {
  23.             _timer.Start();
  24.         }
  25.         protected override void OnStop()
  26.         {
  27.             _timer.Start();
  28.  
  29.         }
  30.         protected override void OnShutdown()
  31.         {
  32.  
  33.             base.OnShutdown();
  34.             _timer.Stop();
  35.  
  36.         }
  37.         public void newfunction(object obj)
  38.         {
  39.  
  40.             Form fr = new Form();
  41.             fr.Export();
  42.  
  43. //HERE IT IS TAKEN TO ANOTHER CLASS WHERE IT CHECKS IF TODAY IS MONDAY OR FRIDAY AND REST OF THE WORK IS DONE.//
  44.  
  45.  
  46.         }
  47.  
  48.  
  49.     }
  50. }
  51.  
  52.  
Thanks
Ayush
Oct 21 '08 #1
16 1829
MrMancunian
569 Expert 512MB
Is there a question involved in your post? Or do you think we're mindreaders? "Hmmmm...I just see clouds...Oh, well..."

Steven
Oct 21 '08 #2
Yes there is a question involved in the post. the service is not running manually. I have to start it every time it has to generate that file.
Oct 27 '08 #3
Plater
7,872 Expert 4TB
Have you tried putting log entries into the program (just as a test) at every function and such, to see if its ending up in a bad state?
(i.e. closed, or the timer is stopped or whatever)
Oct 27 '08 #4
Thanks for the reply .no i did not put any log entries. how do I do that.
Oct 27 '08 #5
Plater
7,872 Expert 4TB
Umm, create a function that logs to a file, and then call that function from other sections of your code.

Such as:
MyLoggingFunction("Startup");

MyLoggingFunction("Checking time");

MyLoggingFunction("Timer stopping");

MyLoggingFunction("service stopping");
Oct 27 '08 #6
tlhintoq
3,525 Expert 2GB
Hi,

I have tried several ways to make this work but only found work arounds. so I am posting this as a last trial.

I have a windows service which has to kick start and generate a file and email it to our clients every monday and friday.

the service does this only when i stop it and start it. but it has to do this without someone starting it. i know we use a timer for this. i did too. but i dont know whats the issue. the timer is supposed to start everyday and check if its a monday or friday which it is not doing.

below is my code



Expand|Select|Wrap|Line Numbers
  1. namespace export
  2. {
  3.     public partial class ExportService1 : ServiceBase
  4.     {
  5.  
  6.         Timer _timer = new Timer();
  7.         ServiceController controller = new ServiceController();
  8.  
  9.         public ExportService1()
  10.         {
  11.             _timer = new Timer(89000000);
  12.             InitializeComponent();
  13.         }
  14.  
  15.         protected override void OnStart(string[] args)
  16.         {
  17.  
  18.             _timer.Start();
  19.             newfunction(null);
  20.         }
  21.         protected override void OnContinue()
  22.         {
  23.             _timer.Start();
  24.         }
  25.         protected override void OnStop()
  26.         {
  27.             _timer.Start();
  28.  
  29.         }
  30.         protected override void OnShutdown()
  31.         {
  32.  
  33.             base.OnShutdown();
  34.             _timer.Stop();
  35.  
  36.         }
  37.         public void newfunction(object obj)
  38.         {
  39.  
  40.             Form fr = new Form();
  41.             fr.Export();
  42.  
  43. //HERE IT IS TAKEN TO ANOTHER CLASS WHERE IT CHECKS IF TODAY IS MONDAY OR FRIDAY AND REST OF THE WORK IS DONE.//
  44.  
  45.  
  46.         }
  47.  
  48.  
  49.     }
  50. }
  51.  
  52.  
Thanks
Ayush
So when you stop and you start, the program works.
It doesn't work automatically each time the timer ticks off.
Then the timer isn't working as you think it should, in one respect or another.

I notice you say "The timer starts every day." It really shouldn't. The timer should be running all the time. It doesn't 'start' every day; it 'Tick's every time the interval setting in milliseconds is reached. Saying that it 'starts' every day implies that it stops every day as well. If the timer stops, then something has to cause it to start again. Maybe this is the problem without going any further. Maybe your called method stops the timer and nothing starts it up again.

But, assuming that's not the case... Where else could it be performing differently than expected?

To start with, the interval for 24 hours should be 86,400,000 milliseconds.
You have 89000000 which is...
/1000ms = 890000 seconds
/60 = 1483.333 minutes
/60 = 24.722222 hours or nearly 3/4 of an hour longer than a day.

So if you are expecting your program to execute at the same time every day; it won't. Its going to creep by .7222 hours each time the timer ticks off 24.722 hours. After 4 ticks it will be off nearly 3 hours (2.888888). By the end of the 7 day week it will be off more than 5 hours. So maybe it would carry out as expected, if it were giving the extra time to reach its countdown. I presume when it doesn't work AT 24 hours you say to yourself [b]"What the frack!"[/B] and kill it, then start it manually and it works fine.

There is no code shown for the method called when the timer actually ticks.
Should look something like...

Expand|Select|Wrap|Line Numbers
  1. private void _timer_Tick(object sender, EventArgs e)
  2. {
  3. // This happens every 24.722222 hours
  4. }
When you create the timer, you also don't seem to be assigning a method to the Tick event that is raised when those 89000000ms elapse.

Should look something like...

Expand|Select|Wrap|Line Numbers
  1. _timer.Tick += new System.EventHandler(this._timer_Tick);
So maybe its not performing as expected, because you don't have a method telling it to perform, or you don't call that method when the timer elapses?


Regards,
tlhIn'toQ
Oct 27 '08 #7
Plater
7,872 Expert 4TB
What kind of Timer object is that?
You should be using Sytem.Threading.Timer for a service application.
Oct 27 '08 #8
i created a log file and checked and its writing to the file only once when the service has started. the timer has to kick off after every 1 min and write to the log file which its not doing. my code is the same that i posted before but I have just called the log function after start stop etc events
Oct 27 '08 #9
thanks tlhintoq for your reply I will try to implement what you said.
I am using System.Timers; doesn't this work with windows services?
Oct 27 '08 #10
Curtis Rutland
3,256 Expert 2GB
So did you even read tlhIn'toQ's post?
Oct 27 '08 #11
I did and I am working according to that.
Oct 27 '08 #12
Plater
7,872 Expert 4TB
I am using System.Timers; doesn't this work with windows services?
I am pretty sure System.Timers.Timer still relies on a windows message to control itself, which I don't think services get (might be wrong)

The System.Threading.Timer is the most complex of the 3 main Timer objects, and can be tricky to understand at first, but the MSDN should still have a good example on how to use it.
Oct 27 '08 #13
got it. I will look in to the exaomple and try to change it. I will let you know if it worked.
Oct 27 '08 #14
Is there a question involved in your post? Or do you think we're mindreaders? "Hmmmm...I just see clouds...Oh, well..."

Steven
MrMancunian it is absolutly fine if you dont reply if at all you dont understand the question.I wont mind.Please don't be rude. remeber that there are 1000's of other sites available and thousands of other people ready to help you are not the only one out there.
Oct 27 '08 #15
MrMancunian
569 Expert 512MB
MrMancunian it is absolutly fine if you dont reply if at all you dont understand the question.I wont mind.Please don't be rude. remeber that there are 1000's of other sites available and thousands of other people ready to help you are not the only one out there.
I wasn't being rude, I was pointing out that there was no question in your first post.

the service is not running manually. I have to start it every time it has to generate that file.
Even in your second post, there was no question. A question, my friend, is a sentence that end with a questionmark.

How are we supposed to know what you want when you don't tell us? (Now thát is a question!)

Steven
Oct 28 '08 #16
Plater
7,872 Expert 4TB
I wasn't being rude, I was pointing out that there was no question in your first post.
Both of you knock it off, it isn't helpful.
The original post did not straight forwardly say "I am having trouble with this, this is what i did, how can i correct it?"
But it is possible to determine what the question was from the title and the post content.
A servuice is supposed to be running and every day at a certain time perform the action, however it is not doing that. The only time it "works" is if you do a start/stop.
Which, based on the timer sample code, would suggest that the timer tick() event is not getting fired correctly.

Now then, as an afterthought, are you remember to uninstall and re-install your service when you make changes? It's a pretty common thing to forget. (Like it took me a few hours to realize I needed to do that the first time I tried to make a service)

Edit:
I built a test service with a System.Threading.Timer object with a 5second interval. Every 5 seconds it would make a log entry. I had no troubles with it not firing.
Oct 28 '08 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Simon Harvey | last post by:
Hi everyone, I need to make a service that monitors a directory for changes in the files contained within it. I have two questions: 1. I'm going to be using a FileSystemWatcher object to do...
3
by: belgiozen | last post by:
Hi, I have a working windows service,it is looking for files on the disk and when some of the files are cupdated it calls an executable. But it takes a lot of time(about 10 minutes) to run the...
4
by: Blaxer | last post by:
I have read approximately 30 articles now on various methods to make your vb.net application automatically update itself and I can't see how they apply to a vb.net windows services projects. The...
6
by: Joe | last post by:
C# .NET 2.0 I've got a Windows Service that works exactly as I expect it to. However, it does rely on a few Configuration settings. If the customer doesn't configure it correctly, the service...
4
by: Ashley Bryant | last post by:
I've created a Windows Service in vb.net that is set up to run automatically, but it never starts during system startup. I can start it manually, but I need the service to start without any user...
7
by: Joseph Geretz | last post by:
I have a Service which runs OK, but I'm abviously not starting it properly. In my OnStart event I commence a long running process which polls a database table and performs various processing. Since...
3
by: Maqsood Ahmed | last post by:
I have made a windows service which is configured to start automatically at the system startup under the USER account type. The service is working perfectly fine when I try to start it manually,...
3
by: Mika M | last post by:
I'm programming quite simple Windows Service using C# 2005. I also made setup for it into same solution. When I run setup to install service, it's going fine into C:\Program Files\MyCompany\My...
4
by: =?Utf-8?B?am1hZ2FyYW0=?= | last post by:
I've written a .net Windows service in C#. It works most of the time for most people. It is configured to automatically start when Windows boots. Sometimes it fails to start on Windows XP during...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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...
0
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...
0
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...

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.