本文整理汇总了C#中System.ServiceProcess.ServiceController.WaitForStatus方法的典型用法代码示例。如果您正苦于以下问题:C# ServiceController.WaitForStatus方法的具体用法?C# ServiceController.WaitForStatus怎么用?C# ServiceController.WaitForStatus使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.ServiceProcess.ServiceController
的用法示例。
在下文中一共展示了ServiceController.WaitForStatus方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RestartSB
public static void RestartSB()
{
string ServiceName = "SolutionBuilder Core Service";
ServiceController service = new ServiceController();
service.MachineName = ".";
service.ServiceName = ServiceName;
string status = service.Status.ToString();
try
{
if (service.Status == ServiceControllerStatus.Running)
{
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped);
}
//Recycle App Pool
try
{
RecycleAppPool();
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
if (service.Status == ServiceControllerStatus.Stopped)
{
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running);
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
示例2: StartService
/// <summary>
/// Start the service with the given name and wait until the status of the service is running.
/// If the service status is not running after the given timeout then the service is considered not started.
/// You can call this method after stop or pause the service in order to re-start it.
/// </summary>
/// <param name="serviceName">The name of the service</param>
/// <param name="timeout">The timeout.</param>
/// <returns>True if the service has been started. Otherwise, false.</returns>
public static bool StartService(string serviceName, TimeSpan timeout)
{
try
{
bool timeoutEnabled = (timeout.CompareTo(TimeSpan.Zero) > 0);
using (ServiceController c = new ServiceController(serviceName))
{
c.Refresh();
if (timeoutEnabled && c.Status == ServiceControllerStatus.Running)
return true;
if (!timeoutEnabled && (c.Status == ServiceControllerStatus.Running || c.Status == ServiceControllerStatus.StartPending || c.Status == ServiceControllerStatus.ContinuePending))
return true;
if (c.Status == ServiceControllerStatus.Paused || c.Status == ServiceControllerStatus.ContinuePending)
c.Continue();
else if (c.Status == ServiceControllerStatus.Stopped || c.Status == ServiceControllerStatus.StartPending)
c.Start();
if (timeoutEnabled)
c.WaitForStatus(ServiceControllerStatus.Running, timeout);
return true;
}
}
catch (Exception e)
{
Utils.Trace(e, "Unexpected error starting service {0}.", serviceName);
return false;
}
}
示例3: Restart
public bool Restart(string[] args, TimeSpan timeout)
{
var service = new ServiceController(serviceName);
try
{
var millisec1 = TimeSpan.FromMilliseconds(Environment.TickCount);
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
Log.Info("Service is stopped");
// count the rest of the timeout
var millisec2 = TimeSpan.FromMilliseconds(Environment.TickCount);
timeout = timeout - (millisec2 - millisec1);
service.Start(args);
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
Log.Info("Service has started");
return true;
}
catch (Exception ex)
{
Log.ErrorException("Cannot restart service " + serviceName, ex);
return false;
}
}
示例4: RestartService
public void RestartService(int timeoutMilliseconds)
{
log.Info(string.Format("Restarting {0}", serviceName));
var service = new ServiceController(serviceName, machineName);
try
{
int millisec1 = Environment.TickCount;
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
log.Debug(string.Format("Stopped {0}", serviceName));
// count the rest of the timeout
int millisec2 = Environment.TickCount;
timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds - (millisec2 - millisec1));
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
log.Debug(string.Format("Started {0}", serviceName));
}
catch (Exception ex)
{
log.Error(ex);
}
}
示例5: RestartSmartComService
/// <summary>
/// Перезапустить службу SmartCOM.
/// </summary>
/// <param name="timeout">Ограничение по времени для перезапуска службы SmartCOM.</param>
public static void RestartSmartComService(TimeSpan timeout)
{
var service = new ServiceController("SmartCom2");
var msStarting = Environment.TickCount;
var waitIndefinitely = timeout == TimeSpan.Zero;
if (service.CanStop)
{
//this.AddDebugLog(LocalizedStrings.Str1891);
service.Stop();
}
if (waitIndefinitely)
service.WaitForStatus(ServiceControllerStatus.Stopped);
else
service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
//this.AddDebugLog(LocalizedStrings.Str1892);
var msStarted = Environment.TickCount;
timeout = timeout - TimeSpan.FromMilliseconds((msStarted - msStarting));
//this.AddDebugLog(LocalizedStrings.Str1893);
service.Start();
if (waitIndefinitely)
service.WaitForStatus(ServiceControllerStatus.Running);
else
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
//this.AddDebugLog(LocalizedStrings.Str1894);
}
示例6: RestartMemCache
private static void RestartMemCache()
{
// Memcached service
using (ServiceController memCacheService = new ServiceController("memcached Server"))
{
if (memCacheService != null && memCacheService.Container != null)
{
TimeSpan timeout = TimeSpan.FromMilliseconds(10000);
// Stop the memcached service
if (memCacheService.CanStop)
{
memCacheService.Stop();
memCacheService.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
}
// start memcached service
if (memCacheService.Status != ServiceControllerStatus.Running)
{
memCacheService.Start();
memCacheService.WaitForStatus(ServiceControllerStatus.Running, timeout);
}
}
}
}
示例7: RestartWindowsService
public static void RestartWindowsService(string machiname, string serviceName, int timeoutMilliseconds)
{
ServiceController service = new ServiceController(serviceName, machiname);
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
try
{
if ((service.Status.Equals(ServiceControllerStatus.Stopped)) || (service.Status.Equals(ServiceControllerStatus.StopPending)))
{
service.Start();
}
else
{
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
}
MessageBox.Show("Restart yapıldı");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
示例8: Main
static void Main(string[] args)
{
using (RegistryKey rk = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
{
using (var waK = rk.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\WindowsUpdate\\Auto Update", true))
{
if (waK.GetSubKeyNames().Contains("RebootRequired"))
{
Console.WriteLine("Deleting RebootRequired key");
waK.DeleteSubKeyTree("RebootRequired");
Console.WriteLine("Stopping {0} service...", WAserviceName);
ServiceController service = new ServiceController(WAserviceName);
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped);
Console.WriteLine("Starting {0} service...", WAserviceName);
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running);
}
else
Console.WriteLine("Nothing to do");
}
}
}
示例9: Main
public static void Main(string[] args)
{
Eager.Initalize();
var service = new ServiceController("fogservice");
const string logName = "Update Helper";
Log.Entry(logName, "Shutting down service...");
//Stop the service
if (service.Status == ServiceControllerStatus.Running)
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped);
Log.Entry(logName, "Killing remaining FOG processes...");
if (Process.GetProcessesByName("FOGService").Length > 0)
foreach (var process in Process.GetProcessesByName("FOGService"))
process.Kill();
Log.Entry(logName, "Applying MSI...");
ApplyUpdates();
//Start the service
Log.Entry(logName, "Starting service...");
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running);
service.Dispose();
if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + @"\updating.info"))
File.Delete(AppDomain.CurrentDomain.BaseDirectory + @"\updating.info");
}
示例10: MonitorServiceStart
public void MonitorServiceStart()
{
ServiceController cs = new ServiceController();
ServiceController cgw = new ServiceController();
try
{
cs.ServiceName = "HUAWEI SMC 2.0 MonitorManage";
cs.Refresh();
cgw.ServiceName = "HUAWEI SMC 2.0 ConvergeGateway";
cgw.Refresh();
if (cgw.Status == ServiceControllerStatus.Running || cgw.Status == ServiceControllerStatus.StartPending) //监控服务自启动的前提是CGW服务在线
{
//if (cs.Status != ServiceControllerStatus.Running && cs.Status != ServiceControllerStatus.StartPending)
if (cs.Status == ServiceControllerStatus.Stopped)
{
//Thread.Sleep(1000);
TimeSpan timeout = TimeSpan.FromMilliseconds(CgwConst.WAIT_MONITOR_SERVICE_RUNNING_MILLI_SECONDS);
cs.Start();
cs.WaitForStatus(ServiceControllerStatus.Running, timeout);
}
}
}
catch (System.Exception)
{
TimeSpan timeout = TimeSpan.FromMilliseconds(CgwConst.WAIT_MONITOR_SERVICE_RUNNING_MILLI_SECONDS);
cs.Start();
cs.WaitForStatus(ServiceControllerStatus.Running, timeout);
}
}
示例11: Execute
public override void Execute(HostArguments args)
{
if (!ServiceUtils.IsServiceInstalled(args.ServiceName))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Out.WriteLine("The '{0}' service is not installed.", args.ServiceName);
Console.ResetColor();
return;
}
var stopController = new ServiceController(args.ServiceName);
if (stopController.Status == ServiceControllerStatus.Running)
{
stopController.Stop();
stopController.WaitForStatus(ServiceControllerStatus.Stopped);
}
if (stopController.Status != ServiceControllerStatus.Running)
{
stopController.Start();
stopController.WaitForStatus(ServiceControllerStatus.Running);
}
Console.Out.WriteLine("Service restarted");
}
示例12: Execute
public Command Execute()
{
var service = new ServiceController("PlexConnect");
try
{
TimeSpan timeout = TimeSpan.FromMilliseconds(10000);
if (service.Status == ServiceControllerStatus.Running)
{
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
}
if (service.Status == ServiceControllerStatus.Stopped)
{
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
}
}
catch (Exception ex)
{
command.Message = ex.Message;
}
command.Message = "Plex Connect Restarted";
Notify();
return command;
}
示例13: RestartCallButlerService
public static void RestartCallButlerService(string server)
{
if (server.ToLower().Equals("localhost"))
{
server = "127.0.0.1";
}
global::Controls.LoadingDialog.ShowDialog(null, CallButler.Manager.Utils.PrivateLabelUtils.ReplaceProductName(Properties.LocalizedStrings.MainForm_StartingService), Properties.Resources.loading, false, 0);
try
{
ServiceController sc = new ServiceController("CallButler Service", server);
if (sc.Status != ServiceControllerStatus.Stopped)
{
sc.Stop();
sc.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(10));
}
sc.Start();
sc.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Running, TimeSpan.FromSeconds(10));
global::Controls.LoadingDialog.HideDialog();
}
catch
{
try
{
Process [] processes = Process.GetProcessesByName("CallButler Service");
if (processes.Length > 0)
{
processes[0].Kill();
}
string cbServicePath = WOSI.Utilities.FileUtils.GetApplicationRelativePath("") + "\\..\\Service\\CallButler Service.exe";
if (System.IO.File.Exists(cbServicePath))
{
System.Diagnostics.Process.Start(cbServicePath, "-a");
// Wait a few seconds for the app to start up
System.Threading.Thread.Sleep(5000);
global::Controls.LoadingDialog.HideDialog();
}
else
{
global::Controls.LoadingDialog.HideDialog();
System.Windows.Forms.MessageBox.Show(null, "Unable to restart CallButler Service", "Service Restart Failed", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
}
}
catch(Exception ex)
{
global::Controls.LoadingDialog.HideDialog();
System.Windows.Forms.MessageBox.Show(null, "Unable to restart CallButler Service", "Service Restart Failed", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
}
}
}
示例14: run_service_lifetime
public void run_service_lifetime()
{
_fileName = @"C:\Temp\dummyout.txt";
if (File.Exists(_fileName)) File.Delete(_fileName);
Call("WinServiceWrapper.exe", "install start");
var service = new ServiceController("MyAppsServiceName");
service.WaitForStatus(ServiceControllerStatus.Running);
KillHostedProcesses();
Thread.Sleep(1000);
service.WaitForStatus(ServiceControllerStatus.Running);
}
示例15: Main
static void Main(string[] args)
{
(new Logger()).WriteNotice("IIS restarting: begin");
try
{
System.Diagnostics.Process cIISreset = new System.Diagnostics.Process();
cIISreset.StartInfo.FileName = Environment.GetFolderPath(Environment.SpecialFolder.System) + @"\iisreset.exe";
cIISreset.Start();
(new Logger()).WriteDebug("iisreset.exe started...");
System.Diagnostics.Process[] aPPP;
while (true)
{
System.Threading.Thread.Sleep(1000);
aPPP = System.Diagnostics.Process.GetProcesses();
if (null == aPPP.FirstOrDefault(o => o.ProcessName == "iisreset"))
break;
}
(new Logger()).WriteNotice("IIS restarted");
}
catch (Exception ex)
{
(new Logger()).WriteError(ex);
}
(new Logger()).WriteNotice("IG restarting: begin");
try
{
ServiceController controller = new ServiceController();
controller.ServiceName = "InGenie.Initiator"; // i.e “w3svc”
if (controller.Status != ServiceControllerStatus.Running)
{
controller.Start();
}
else
{
controller.Stop();
(new Logger()).WriteDebug("InGenie.Initiator stopping...");
controller.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 0, 10));
System.Threading.Thread.Sleep(1000);
(new Logger()).WriteDebug("InGenie.Initiator stopped...");
controller.Start();
}
(new Logger()).WriteDebug("InGenie.Initiator starting...");
controller.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 10));
(new Logger()).WriteNotice("InGenie.Initiator restarted");
}
catch (Exception ex)
{
(new Logger()).WriteError(ex);
}
System.Threading.Thread.Sleep(1000);
}