1

can any one help me up on ...how can i restart my window service programatically by every 15 min in c#.net please help me .. i have done in my code like diz way i have did like dis way until upto in class page[RunInstaller(true)]

public class ProjectInstaller : System.Configuration.Install.Installer private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1; private System.ServiceProcess.ServiceInstaller serviceInstaller1; public ProjectInstaller() InitializeComponent(); } private void InitializeComponent() { this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller(); this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller(); this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem; this.serviceProcessInstaller1.Password = null; this.serviceProcessInstaller1.Username = null; this.serviceInstaller1.ServiceName = "MyNewService"; this.serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic; this.Installers.AddRange (new System.Configuration.Install.Installer[] { this.serviceInstaller1, this.serviceInstaller1}); } } 
4
  • Why do you need to restart it every 15 min? If you want code to run every 15 minutes, use a timer and set the timer Tick event to the code that should run. Commented Feb 26, 2011 at 14:59
  • Restarting the service every 15 minutes sounds like you are trying to solve something else. Exactly why do you want to restart your service? Services are designed to be running continuously for long long times, if that is not what you want services might be the wrong tool for the job. Commented Feb 26, 2011 at 15:17
  • By the way, what does your windows service has to do with the asp.net tag you have tagged your question with? You should remove that tag if not relevant to the question. Commented Feb 26, 2011 at 15:18
  • @albin ...thnks fr replying yup your right !! in a particular root folder some files vill come and fall in tht and my services will start the job fine , job completed fr now again after some time some files will comes to that root folder at that particular time i was doin by manually restart so that i m asking Commented Feb 26, 2011 at 15:25

4 Answers 4

4

You can use this code for service restarting:

using System.Diagnostics; public static void RestartService(string serviceName) { var psi = new ProcessStartInfo("net.exe", "stop " + serviceName); psi.WindowStyle = ProcessWindowStyle.Hidden; psi.UseShellExecute = true; psi.WorkingDirectory = Environment.SystemDirectory; var st = Process.Start(psi); st.WaitForExit(); psi = new ProcessStartInfo("net.exe", "start " + serviceName); psi.UseShellExecute = true; psi.WindowStyle = ProcessWindowStyle.Hidden; psi.WorkingDirectory = Environment.SystemDirectory; st = Process.Start(psi); st.WaitForExit(); 

}

I got this code here, tested it and it worked. Use a timer with 15 minutes interval to call it every 15 minutes.

Sign up to request clarification or add additional context in comments.

Comments

1

Few ways you could do this:

  1. Change your service to be a light-weight stub that hosts your current process in an AppDomain. Use a timer service to unload and restart your AppDomain.

  2. Create two services. Make the one service a timer and programatically restart this service using the ServiceController to access your service to stop and restart it.

Comments

0

You should not restart your complete service.
Instead you can create a timer within your service that triggers at regular intervals

Like this

private void StartTimer() { System.Threading.Timer timer = new System.Threading.Timer( TimerCompleted, null, TimeSpan.FromMinutes(15), TimeSpan.FromMinutes(15)); } private void TimerCompleted(object state) { // Call your action here ProcessFiles(); } 

Call StartTimer from your service start and put all your work in ProcessFiles.

But if you actually just want to monitor changes in a directory you might instead use a FileSystemWatcher. It can notify you of changes in the filesystem immediately when the occur.

1 Comment

What's up with the sudden downvote? Here on SO it is good manners to write a comment when downvoting to point out possible improvements.
0
  • Create a batch file with following command:

    net stop "ServiceName" net start "ServiceName"

  • Use windows scheduler and create a schedule which runs this script every 15 mins.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.