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});
   }
}
Albin Sunnanbo
  • 46,430
  • 8
  • 69
  • 108
praveen
  • 63
  • 4
  • 12
  • 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. – mellamokb Feb 26 '11 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. – Albin Sunnanbo Feb 26 '11 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. – Albin Sunnanbo Feb 26 '11 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 – praveen Feb 26 '11 at 15:25

4 Answers4

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.

Community
  • 1
  • 1
Sajib Mahmood
  • 3,382
  • 3
  • 37
  • 50
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.

bryanbcook
  • 16,210
  • 2
  • 40
  • 69
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.

Albin Sunnanbo
  • 46,430
  • 8
  • 69
  • 108
  • 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. – Albin Sunnanbo Mar 08 '11 at 09:22
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.

Amit Rai Sharma
  • 4,015
  • 1
  • 28
  • 35