本文整理汇总了C#中System.ServiceProcess.ServiceController.Start方法的典型用法代码示例。如果您正苦于以下问题:C# ServiceController.Start方法的具体用法?C# ServiceController.Start怎么用?C# ServiceController.Start使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.ServiceProcess.ServiceController
的用法示例。
在下文中一共展示了ServiceController.Start方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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;
}
}
示例2: 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);
}
}
示例3: serviceStart
private static void serviceStart()
{
ServiceController[] scServices;
scServices = ServiceController.GetServices();
foreach (ServiceController scTemp in scServices)
{
if (scTemp.ServiceName == "open-audit-check-service")
{
ServiceController sc = new ServiceController("open-audit-check-service");
if (sc.Status == ServiceControllerStatus.Stopped)
{
int tries = 0;
sc.Start();
while (sc.Status == ServiceControllerStatus.Stopped && tries < 5)
{
Thread.Sleep(1000);
sc.Refresh();
tries++;
}
}
}
}
}
示例4: InstallService
/// <summary>
/// 安装服务:
/// </summary>
/// <param name="filepath"></param>
public void InstallService(string filepath)
{
try
{
string serviceName = GetServiceName(filepath);
ServiceController service = new ServiceController(serviceName);
if (!ServiceIsExisted(serviceName))
{
//Install Service
AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller() { UseNewContext = true, Path = filepath };
myAssemblyInstaller.Install(new Hashtable());
myAssemblyInstaller.Commit(new Hashtable());
myAssemblyInstaller.Dispose();
//--Start Service
service.Start();
}
else
{
if (service.Status != ServiceControllerStatus.Running && service.Status != ServiceControllerStatus.StartPending)
{
service.Start();
}
}
}
catch (Exception ex)
{
throw new Exception("installServiceError/n" + ex.Message);
}
}
示例5: 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);
}
}
示例6: 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);
}
}
示例7: StartService
public static string StartService(string servicename, string[] parameters = null)
{
ServiceController sc = new ServiceController(servicename);
if (sc.Status == ServiceControllerStatus.Running) return "Running";
try
{
if (parameters != null)
{
sc.Start(parameters);
}
else
{
sc.Start();
}
return CheckServiceCondition(servicename);
}
catch (Exception e)
{
//Service can't start because you don't have rights, or its locked by another proces or application
return e.Message;
}
}
示例8: 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);
}
示例9: runServiceCheck
public void runServiceCheck()
{
Utils util = new Utils();
ServiceController sc = null;
try
{
sc = new ServiceController(serviceName);
switch (sc.Status)
{
case ServiceControllerStatus.Running:
break;
case ServiceControllerStatus.Stopped:
sc.Start();
util.writeToLogFile("service checker starting "+serviceName);
break;
case ServiceControllerStatus.Paused:
sc.Stop();
sc.Start();
util.writeToLogFile("service checker starting " + serviceName);
break;
case ServiceControllerStatus.StopPending:
sc.Stop();
sc.Start();
util.writeToLogFile("service checker starting " + serviceName);
break;
case ServiceControllerStatus.StartPending:
sc.Stop();
sc.Start();
util.writeToLogFile("service checker starting " + serviceName);
break;
default:
break;
}
}
catch (Exception e)
{
util.writeEventLog(e.Message);
util.writeEventLog(e.StackTrace);
}
finally
{
try
{
if (sc != null) sc.Close();
} catch (Exception) {}
}
}
示例10: Restart
public static void Restart()
{
ServiceController service = new ServiceController("Spooler");
if ((service.Status.Equals(ServiceControllerStatus.Stopped)) || (service.Status.Equals(ServiceControllerStatus.StopPending)))
{
service.Start();
}
else
{
service.Stop();
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running);
}
}
示例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: Main
/// <summary>
/// The main entry point for the service.
/// </summary>
static void Main(string[] args)
{
if (System.Environment.UserInteractive)
{
try
{
// Arguments used by the installer
ServiceController controller;
string parameter = string.Concat(args);
switch (parameter)
{
case "--install":
ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
controller = new ServiceController("Wev HTTP Listener");
controller.Start();
break;
case "--uninstall":
controller = new ServiceController("Wev HTTP Listener");
controller.Stop();
ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location });
break;
}
}
catch (Exception) { }
}
else
{
ServiceBase.Run(new WevService());
}
}
示例13: StartService
public static int StartService(string strServiceName,
out string strError)
{
strError = "";
ServiceController service = new ServiceController(strServiceName);
try
{
TimeSpan timeout = TimeSpan.FromMinutes(2);
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
}
catch (Exception ex)
{
if (GetNativeErrorCode(ex) == 1060)
{
strError = "服务不存在";
return -1;
}
else if (GetNativeErrorCode(ex) == 1056)
{
strError = "调用前已经启动了";
return 0;
}
else
{
strError = ExceptionUtil.GetAutoText(ex);
return -1;
}
}
return 1;
}
示例14: serviceInstaller1_AfterInstall
private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)
{
using (ServiceController sc = new ServiceController(serviceInstaller1.ServiceName))
{
sc.Start();
}
}
示例15: 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);
}
}
}
}