本文整理汇总了C#中System.ServiceProcess.ServiceController.Close方法的典型用法代码示例。如果您正苦于以下问题:C# ServiceController.Close方法的具体用法?C# ServiceController.Close怎么用?C# ServiceController.Close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.ServiceProcess.ServiceController
的用法示例。
在下文中一共展示了ServiceController.Close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ServiceInstaller_Committed
/// <summary>
/// Handles the Committed event of the ServiceInstaller control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Configuration.Install.InstallEventArgs"/> instance containing the event data.</param>
private void ServiceInstaller_Committed(object sender, InstallEventArgs e)
{
using (var service = new ServiceController(this.ServiceInstaller.ServiceName))
{
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running);
service.Close();
}
}
示例2: serviceInstaller1_BeforeUninstall
private void serviceInstaller1_BeforeUninstall(object sender, InstallEventArgs e)
{
ServiceController sc = new ServiceController("Ezector Thinkingcap FTP");
if (sc.Status == ServiceControllerStatus.Running)
{
sc.Stop();
sc.WaitForStatus(ServiceControllerStatus.Stopped);
sc.Close();
}
}
示例3: btnRestart_Click
private void btnRestart_Click(object sender, RoutedEventArgs e)
{
ServiceStruct ss = (ServiceStruct)lv.SelectedItem;
ServiceController sc = new ServiceController(ss.name);
sc.Stop();
sc.WaitForStatus(ServiceControllerStatus.Stopped);
sc.Start();
sc.WaitForStatus(ServiceControllerStatus.Running);
sc.Close();
GetAllServices();
}
示例4: checkServiceInstallation
/// <summary>
/// Verifies if the Window service with the given name is installed.
/// </summary>
/// <param name="serviceName"></param>
/// <returns>true if the service is installed properly. false otherwise</returns>
public static bool checkServiceInstallation(string serviceName)
{
bool exists = false;
try
{
ServiceController sc = new ServiceController(serviceName);
sc.Refresh(); //just a dummy call to make sure the service exists.
sc.Close();
sc = null;
exists = true;
}
catch
{}
return exists;
}
示例5: Start
public void Start(string serviceName)
{
_logger.Log(String.Format("Starting '{0}' Service ...", serviceName));
var serviceController = new ServiceController(serviceName);
if (serviceController.Status == ServiceControllerStatus.Running)
{
_logger.Log(String.Format("'{0}' service already started.", serviceName));
return;
}
serviceController.Start();
serviceController.WaitForStatus(ServiceControllerStatus.Running);
serviceController.Close();
_logger.Log(String.Format("Service '{0}' started and now running ...", serviceName));
}
示例6: CheckServiceInstallation
/// <summary>
/// Verifies if the Window service with the given name is installed.
/// </summary>
/// <param name="serviceName">Name of the service.</param>
/// <returns>
/// true if the service is installed properly. false otherwise
/// </returns>
public static bool CheckServiceInstallation(string serviceName)
{
bool exists = false;
ServiceController sc = null;
try
{
sc = new ServiceController(serviceName);
sc.Refresh(); //just a dummy call to make sure the service exists.
exists = true;
}
finally
{
if (sc != null)
sc.Close();
sc = null;
}
return exists;
}
示例7: OnBeforeUninstall
protected override void OnBeforeUninstall(IDictionary savedState)
{
ServiceController service = new ServiceController(serviceInstaller.ServiceName);
try
{
if (service.Status == ServiceControllerStatus.Running)
{
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 0, 0, 10));
service.Close();
}
}
finally
{
base.OnBeforeUninstall(savedState);
}
}
示例8: OnCustomCommand
/// <summary>
/// 执行自定义重置命令
/// </summary>
/// <param name="command"></param>
protected override void OnCustomCommand(int command)
{
if (200 != command || isTmRunning)
return;
isTmRunning = true;
ServiceController ctrl = new ServiceController("Granity文件服务");
try
{
LogMessage("守护Granity文件服务", null, EventLogEntryType.Information);
try { ctrl.Stop(); }
catch { }
Thread.Sleep(new TimeSpan(0, 1, 0));
ctrl.Start();
Thread.Sleep(new TimeSpan(0, 1, 0));
}
catch { }
ctrl.Close();
isTmRunning = false;
}
示例9: Stop
public void Stop(string serviceName)
{
_logger.Log(String.Format("Stopping Service '{0}' ...", serviceName));
var serviceController = new ServiceController(serviceName);
if (serviceController.Status == ServiceControllerStatus.Stopped)
{
_logger.Log(String.Format("Service '{0}' already stopped.", serviceName));
return;
}
if (!serviceController.CanStop)
throw new ApplicationException(
String.Format("Service '{0}' can't be stop at this time, please try again later", serviceName));
serviceController.Stop();
serviceController.WaitForStatus(ServiceControllerStatus.Stopped);
serviceController.Close();
_logger.Log(String.Format("Service '{0}' successfully stopped.", serviceName));
}
示例10: StartStopService
private void StartStopService(bool state)
{
ServiceController service = new ServiceController("AlarmworkflowService");
try
{
if (state)
{
Log.Write("Starting service...");
try
{
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 30));
Log.Write("Service started.");
}
catch (System.ServiceProcess.TimeoutException)
{
Log.Write("Service not started. Please check the Windows Eventlog");
}
}
else
{
Log.Write("Stopping service...");
if (service.Status == ServiceControllerStatus.Running)
{
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped);
}
service.Close();
Log.Write("Service stopped.");
}
}
catch (InvalidOperationException)
{
// This exception is ok - it occurs if the service does not exist
}
}
示例11: Start
public void Start(string serviceName)
{
try
{
_logger.Log(String.Format("Starting '{0}' Service ...", serviceName));
var serviceController = new ServiceController(serviceName);
if (serviceController.Status == ServiceControllerStatus.Running)
{
_logger.Log(String.Format("'{0}' service already started.", serviceName));
return;
}
serviceController.Start();
serviceController.WaitForStatus(ServiceControllerStatus.Running);
serviceController.Close();
_logger.Log(String.Format("Service '{0}' started and now running ...", serviceName));
}
catch (Exception ex)
{
_logger.Log(string.Format("An error occured trying to start the {0} service: {1}", serviceName, ex));
}
}
示例12: StartService
public static void StartService(string name)
{
ServiceController sc = new ServiceController(name);
try
{
if (sc != null && sc.Status == ServiceControllerStatus.Stopped)
{
sc.Start();
}
sc.WaitForStatus(ServiceControllerStatus.Running);
sc.Close();
}
catch (Exception)
{
}
}
示例13: StartIfNeeded
// Method to start the service automatically after installation
private void StartIfNeeded(object sender, InstallEventArgs e)
{
// Do we need to do any work?
if (!m_startOnInstall)
return;
try
{
TimeSpan waitTo = new TimeSpan(0, 0, m_startTimeout);
// Get a handle to the service
ServiceController sc = new ServiceController(base.ServiceName);
// Start the service and wait for it to start
sc.Start();
sc.WaitForStatus(ServiceControllerStatus.Running, waitTo);
// Be good and release our handle
sc.Close();
LogInstallMessage(EventLogEntryType.Information, m_logMessagePrefix + " Service Started");
}
// Catch all exceptions
catch (Exception ex)
{
LogInstallMessage(EventLogEntryType.Error, m_logMessagePrefix + ex.Message);
}
}
示例14: StartService
public static void StartService(string serviceName, string serverName, int timeoutSec)
{
try
{
ServiceController service = new ServiceController(serviceName, serverName);
if (service.Status == ServiceControllerStatus.Stopped || service.Status == ServiceControllerStatus.StartPending)
{
try
{
if (service.Status == ServiceControllerStatus.Stopped)
{
Log.Info("argustv: service {0} is stopped, so we try start it now...", serviceName);
service.Start();
}
service.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, timeoutSec));
}
catch (Exception ex)
{
Log.Error("argustv: starting service {0} failed, {1}", serviceName, ex.Message);
}
}
Log.Info("argustv: service {0} - current status: {1}", serviceName, service.Status.ToString());
service.Close();
service.Dispose();
}
catch
{
Log.Info("argustv: service {0} not found on {1}", serviceName, serverName);
}
}
示例15: StopService
private void StopService()
{
var sc = new ServiceController(_Configuration.ServiceName);
try
{
if (sc.CanStop)
{
if (sc.Status != ServiceControllerStatus.Stopped && sc.Status != ServiceControllerStatus.StopPending)
{
sc.Stop();
sc.WaitForStatus(ServiceControllerStatus.Stopped, _ServiceControllerTimeout);
}
}
}
catch (System.ServiceProcess.TimeoutException)
{
Console.Write(string.Format(CultureInfo.InvariantCulture, "Could not stop the {0} service.", _Configuration.ServiceName));
}
finally
{
sc.Close();
}
}