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();
}
p开发者_StackOverflowrivate 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});
}
}
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.
Few ways you could do this:
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.
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.
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.
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.
精彩评论