.net sukbir

Dear All,

I have a windows service which I need to run on particular time of a day. So I have build my windows service and add the code of my action of checking the database in the onStart function Is this correct or must I have a main function Another thing I have added is the debug.write I dont know whether it prints or not I done with installer and manage to successfully install my window service. So I dont know how to confirm if my windows service is running or not How can I set the time to be like specify for certain hours in a day eg. to run at 10 am, 12 pm, 2pm and 4pm daily Thanks.



Re: Visual C# General Please help me confirm on windows service

TaylorMichaelL

You really should not use a Windows service to do that. If you need to schedule something to run at a specific time of day then create a normal console app and then use the Scheduler to schedule it to run.

If you are adament( ) about using a service then your service is going to have to use a timer to wake itself up when the appropriate time of day arrives. When it wakes up it does whatever it needs to do. Your OnStart method would simply start the timer.

As for running a service it can be set up to auto-start (Automatic) when you install. You'll then need to start it (requires admin privileges). After that it'll run until it is shut down (by an admin) or the machine reboots. If it is set to auto-start then it starts each time the machine does. Debugging a service is not easy as you have to start the service and then use the debugger to break into it. This doesn't allow you to debug the start method though. The alternative, and preferred in my opinion, is to create a service that can also run as a console app. Use a command line param to indicate when the service should run as a normal app. You have to do some work to mimic the SCM behavior of the service while in console mode but you can then debug normally. Just be aware that security changes when running as a service.

Michael Taylor - 2/1/07
http://p3net.mvps.org





Re: Visual C# General Please help me confirm on windows service

.net sukbir

Dear Michael,

I dont quite get you when you said run a console as service application u mean to say that every time a pop of console will come out when it runs is it Can you explain more thanks.





Re: Visual C# General Please help me confirm on windows service

RizwanSharp

I would recomend to let Windows Service run always and use a config file which may contain the times on which to do DB operations.

After some interval (1 Minute or so) service should read the config file and see if its time or close to time (+ -30 secs) to do DB operations then do that otherwise keep checking the Config file to reach the appropriate time.

in OnStart method simply start a thread and do all the above mentioned stuff in that thread.

I hope this will help.

Best Regards,

Rizwan aka RizwanSharp






Re: Visual C# General Please help me confirm on windows service

.net sukbir

Dear Rizwan,

Can you show me some example of how the config files works and where to store it also pls. Thanks.





Re: Visual C# General Please help me confirm on windows service

RizwanSharp

.net sukbir wrote:

Dear Rizwan,

Can you show me some example of how the config files works and where to store it also pls. Thanks.

Config files are nothing more than a text or xml file. You can also use a .txt file to do it.

Like Create a text file manually using notepad and put these times in that:

11:00 am

1:00 pm

etc....

After each 1 minute, Read all the lines of this file in some array using any class e.g StreamReader....

Now compare current time with each of the times which were fetched from that file. If there is a match then do DB work otherwise wait for next iteration... i.e after 1 minute or so...

You can save this file anywhere but you have to provide the complete path to this in your Windows Servce so it can load it and do its work without rasing FileNotFound exception.

I hope this will help.

Best Regards,

Rizwan aka RizwanSharp






Re: Visual C# General Please help me confirm on windows service

.net sukbir

Dear Rizwan,

About the timer I am confuse. Some articles say use Timer some say use Timer.Thread which is much safer So which is which and what is best to use in running a timer for windows service.





Re: Visual C# General Please help me confirm on windows service

RizwanSharp

See My last post, I suggested to start a thread in OnStart method and do all this work in that.

I hope this will help.

Best Regards,

Rizwan aka RizwanSharp






Re: Visual C# General Please help me confirm on windows service

.net sukbir

Dear Rizwan,

What you are saying is that in my onStart to run a timer rite Then run it for every minute is it So then do all my work in that onStart method only is it and even my db operation is to be done in it is it Actually what it means by thread It means a process rite. I would appreaciate if you have any example of my problem solution. Thanks rizwan.





Re: Visual C# General Please help me confirm on windows service

RizwanSharp

Thread is the smallest unit of execution, A Process may have many Threads. You can use thread in your application when you need to run 2 or more simultainous task.

In Windows Service, You may need to run simultainous tasks but to avoid service start failure its better to create and start thread in on Start Method.

using System;

using System.ComponentModel;

using System.ServiceProcess;

using System.Threading;

namespace WindowsService2

{

public partial class Service1 : ServiceBase

{

private Thread doWorkThread;

private bool existThread = false;

public Service1()

{

InitializeComponent();

this.doWorkThread = new Thread(new ThreadStart(DoWork));

}

protected override void OnStart(string[] args)

{

this.doWorkThread.Start(); // Start The Thread

}

protected override void OnStop()

{

this.existThread = true;

}

private void DoWork()

{

while (true)

{

if (this.existThread) // Check if service was stopped

return;

DateTime timeNow = DateTime.Now;

// Load closest coming time from the sime file or DB

if (timeNow .Now.Hour == 12 /* Replace 12 with loaded hour*/ && timeNow.Now.Minute == 0 /* Replace 0 with Loaded Minutes*/) // Compare current time with the time loaded from the file or DB

{

//Do some work

}

Thread.Sleep(TimeSpan.FromSeconds(30)); // Wait for next 30 seconds and the all the stuff again unless service is stopped which is checked by exitThread flag.

}

}

}

}

I hope this will help.

Best Regards,

Rizwan aka RizwanSharp






Re: Visual C# General Please help me confirm on windows service

.net sukbir

Dear Rizwan,

So basically thread is a small process is it. So in this case I only have one thread running rite. There wont be any complexity with this rite. The timespan is kept in the system is it. So the thread will remember is it Sorry ya I am very new to thread so trying to understand as much as possible. Thanks.





Re: Visual C# General Please help me confirm on windows service

RizwanSharp

Yes the smalles part of execution is a thread, an applicaiton may have many threads for parallel processing of work.

e.g:

1 thread of Media Player is playing a movie while other is downloading updates etc..

There are 1000 of live examples.

You can study about threads in MSDN of Joseph Albari's Threading tutorial by searching on google.

Best Regards,

Rizwan aka RizwanSharp






Re: Visual C# General Please help me confirm on windows service

.net sukbir

Dear Rizwan,

I just would like to confirm with you the working of your code. Is that that your code will start to work rite then after once it has done the job in the //Do some work it will go to sleep rite. So with this I can assure there wont be any overlaps rite. So in the case of some database updates it means it will settle it fully then only it will go and sleep rite.

if (timeNow .Now.Hour == 12 /* Replace 12 with loaded hour*/ && timeNow.Now.Minute == 0 /* Replace 0 with Loaded Minutes*/) // Compare current time with the time loaded from the file or DB

{

//Do some work

}





Re: Visual C# General Please help me confirm on windows service

RizwanSharp

Exactly

Best Regards,

Rizwan aka RizwanSharp






Re: Visual C# General Please help me confirm on windows service

dadon

Will the thread start when windows starts
So the code provided should start me off
Is this the code for C# 2.0 Cheers