本文整理汇总了C#中ServiceControllerStatus类的典型用法代码示例。如果您正苦于以下问题:C# ServiceControllerStatus类的具体用法?C# ServiceControllerStatus怎么用?C# ServiceControllerStatus使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ServiceControllerStatus类属于命名空间,在下文中一共展示了ServiceControllerStatus类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetServiceList
/// <summary>
/// 获取服务列表
/// </summary>
/// <returns></returns>
public static IList<ServiceInformation> GetServiceList(string contains, ServiceControllerStatus status)
{
IList<ServiceInformation> servicelist = new List<ServiceInformation>();
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController s in services)
{
if (s.Status != status) continue;
if (string.IsNullOrEmpty(contains))
{
servicelist.Add(new ServiceInformation(s.ServiceName));
}
else
{
if (s.ServiceName != null && s.ServiceName.ToLower().Contains(contains.ToLower()))
{
servicelist.Add(new ServiceInformation(s.ServiceName));
}
else if (s.DisplayName != null && s.DisplayName.ToLower().Contains(contains.ToLower()))
{
servicelist.Add(new ServiceInformation(s.ServiceName));
}
}
}
return servicelist;
}
示例2: ChangeServiceStatus
/// <summary>
/// Checks the status of the given controller, and if it isn't the requested state,
/// performs the given action, and checks the state again.
/// </summary>
/// <param name="controller"></param>
/// <param name="status"></param>
/// <param name="changeStatus"></param>
public static void ChangeServiceStatus(ServiceController controller, ServiceControllerStatus status, Action changeStatus)
{
if (controller.Status == status)
{
Console.Out.WriteLine(controller.ServiceName + " status is good: " + Enum.GetName(typeof(ServiceControllerStatus), status));
return;
}
Console.Out.WriteLine((controller.ServiceName + " status is NOT " + Enum.GetName(typeof(ServiceControllerStatus), status) + ". Changing status..."));
try
{
changeStatus();
}
catch (Win32Exception exception)
{
ThrowUnableToChangeStatus(controller.ServiceName, status, exception);
}
catch (InvalidOperationException exception)
{
ThrowUnableToChangeStatus(controller.ServiceName, status, exception);
}
var timeout = TimeSpan.FromSeconds(3);
controller.WaitForStatus(status, timeout);
if (controller.Status == status)
Console.Out.WriteLine((controller.ServiceName + " status changed successfully."));
else
ThrowUnableToChangeStatus(controller.ServiceName, status);
}
示例3: ControlService
bool ControlService(ServiceControllerStatus status, Action<ServiceController> controlAction)
{
if (controller.Status == status)
{
log.Debug("The {0} service is already in the requested state: {1}", controller.ServiceName, status);
return false;
}
log.Debug("Setting the {0} service to {1}", controller.ServiceName, status);
try
{
controlAction(controller);
}
catch (Exception ex)
{
string message = string.Format("The {0} service could not be set to {1}", controller.ServiceName, status);
throw new InvalidOperationException(message, ex);
}
controller.WaitForStatus(status, timeout);
if (controller.Status == status)
{
log.Debug("The {0} service was set to {1} successfully", controller.ServiceName, status);
}
else
{
string message = string.Format("A timeout occurred waiting for the {0} service to be {1}",
controller.ServiceName,
status);
throw new InvalidOperationException(message);
}
return true;
}
示例4: InitializeTrayIconAndProperties
public void InitializeTrayIconAndProperties()
{
serviceStatus = _serviceManager.GetServiceStatus();
//Set the Tray icon
if (serviceStatus == ServiceControllerStatus.Running)
notifyTrayIcon.Icon = Properties.Resources.TrayIconRunning;
else if (serviceStatus == ServiceControllerStatus.Stopped)
notifyTrayIcon.Icon = Properties.Resources.TrayIconStopped;
else
notifyTrayIcon.Icon = Properties.Resources.TrayIconActive;
//Setup context menu options
trayContextMenu = new ContextMenuStrip();
trayContextMenu.Items.Add(new ToolStripMenuItem() { Name = ActionConstants.REFRESH, Text = "Refresh Status" });
trayContextMenu.Items.Add("-");
_startServiceItem = new ToolStripMenuItem() { Enabled = ServiceControllerStatus.Stopped.Equals(serviceStatus), Name = ActionConstants.START_SERVICE, Text = "Start Service" };
trayContextMenu.Items.Add(_startServiceItem);
_stopServiceItem = new ToolStripMenuItem() { Enabled = ServiceControllerStatus.Running.Equals(serviceStatus), Name = ActionConstants.STOP_SERVICE, Text = "Stop Service" };
trayContextMenu.Items.Add(_stopServiceItem);
trayContextMenu.Items.Add("-");
trayContextMenu.Items.Add(new ToolStripMenuItem() { Name = ActionConstants.SHOW_LOGS, Text = "Show Logs" });
trayContextMenu.Items.Add("-");
trayContextMenu.Items.Add(new ToolStripMenuItem() { Name = "actionExit", Text = "Exit" });
trayContextMenu.ItemClicked += trayContextMenu_ItemClicked;
//Initialize the tray icon here
this.notifyTrayIcon.ContextMenuStrip = trayContextMenu;
}
示例5: MonitorService
internal void MonitorService()
{
serviceStatus = ServiceControllerStatus.Stopped;
System.Threading.Thread.Sleep(3000);
bool first = true;
while (RunMonitor)
{
try
{
ServiceControllerStatus status = PVServiceManager.GetServiceStatus();
if (status != serviceStatus || first || ForceRefresh)
{
SynchronizationContext.Post(new SendOrPostCallback(delegate
{
ServiceStatusChangeNotification(status);
}), null);
//ServiceStatusChangeNotification(svc.Status);
first = ForceRefresh; // avoid race - do it twice
ForceRefresh = false;
}
serviceStatus = status;
}
catch (Exception)
{
SynchronizationContext.Post(new SendOrPostCallback(delegate
{
ServiceStatusChangeNotification(ServiceControllerStatus.Stopped);
}), null);
}
System.Threading.Thread.Sleep(3000);
}
}
示例6: RestoreService
private void RestoreService(ServiceControllerStatus previousStatus)
{
if (previousStatus == ServiceControllerStatus.Running) {
controller.Start();
} else if (previousStatus == ServiceControllerStatus.Paused) {
controller.Pause();
}
}
示例7: serviceTimer_Tick
private void serviceTimer_Tick (object sender, EventArgs e) {
try {
lastError = null;
lastStatus = serviceController.Status;
} catch (Exception ex) {
lastStatus = ServiceControllerStatus.Paused;
lastError = ex;
}
UpdateStatus ();
}
示例8: ThrowUnableToChangeStatus
private static void ThrowUnableToChangeStatus(string serviceName, ServiceControllerStatus status, Exception exception)
{
var message = "Unable to change " + serviceName + " status to " + Enum.GetName(typeof(ServiceControllerStatus), status);
if (exception == null)
{
throw new InvalidOperationException(message);
}
throw new InvalidOperationException(message, exception);
}
示例9: ToServiceStatus
static ServiceStatus ToServiceStatus(ServiceControllerStatus status)
{
switch (status)
{
case ServiceControllerStatus.StopPending:
return ServiceStatus.Stopped;
case ServiceControllerStatus.Stopped:
return ServiceStatus.Stopped;
default:
return ServiceStatus.Started;
}
}
示例10: CheckStatus
private bool CheckStatus(ServiceControllerStatus status)
{
try
{
this.service.Refresh();
return this.service.Status == status;
}
catch (InvalidOperationException)
{
return false;
}
}
示例11: ServiceController
internal ServiceController(string machineName, System.ServiceProcess.NativeMethods.ENUM_SERVICE_STATUS_PROCESS status)
{
this.machineName = ".";
this.name = "";
this.displayName = "";
this.eitherName = "";
if (!SyntaxCheck.CheckMachineName(machineName))
{
throw new ArgumentException(Res.GetString("BadMachineName", new object[] { machineName }));
}
this.machineName = machineName;
this.name = status.serviceName;
this.displayName = status.displayName;
this.commandsAccepted = status.controlsAccepted;
this.status = (ServiceControllerStatus) status.currentState;
this.type = status.serviceType;
this.statusGenerated = true;
}
示例12: TranslateServiceStatus
/// <summary>
/// Maps the status of eam service to value specified in resources.
/// </summary>
/// <param name="status"></param>
/// <returns></returns>
public static String TranslateServiceStatus(ServiceControllerStatus status)
{
if (status == ServiceControllerStatus.Running)
return Resources.Strings.EAMservice_Running;
if (status == ServiceControllerStatus.Stopped)
return Resources.Strings.EAMservice_Stop;
if (status == ServiceControllerStatus.ContinuePending)
return Resources.Strings.EAMservice_ContinuePending;
if (status == ServiceControllerStatus.Paused)
return Resources.Strings.EAMservice_Paused;
if (status == ServiceControllerStatus.PausePending)
return Resources.Strings.EAMservice_PausePending;
if (status == ServiceControllerStatus.StartPending)
return Resources.Strings.EAMservice_StartPending;
if (status == ServiceControllerStatus.StopPending)
return Resources.Strings.EAMservice_StopPending;
return "";
}
示例13: ChangeServiceStatus
/// <summary>
/// Checks the status of the given controller, and if it isn't the requested state,
/// performs the given action, and checks the state again.
/// </summary>
/// <param name="controller"></param>
/// <param name="status"></param>
/// <param name="changeStatus"></param>
public static void ChangeServiceStatus(ServiceController controller, ServiceControllerStatus status, Action changeStatus)
{
if (controller.Status == status)
{
Logger.Debug(controller.ServiceName + " status is good: " + Enum.GetName(typeof(ServiceControllerStatus), status));
return;
}
Logger.Debug(controller.ServiceName + " status is NOT " + Enum.GetName(typeof(ServiceControllerStatus), status) + ". Changing status...");
changeStatus();
var timeout = TimeSpan.FromSeconds(3);
controller.WaitForStatus(status, timeout);
if (controller.Status == status)
Logger.Debug(controller.ServiceName + " status changed successfully.");
else
throw new InvalidOperationException("Unable to change " + controller.ServiceName + " status to " + Enum.GetName(typeof(ServiceControllerStatus), status));
}
示例14: TrySetStatus
private Task<bool> TrySetStatus(ServiceControllerStatus status, Action method)
{
return Task.Run(() =>
{
var timeSpan = TimeSpan.FromSeconds(WaitTime);
try
{
method();
this.service.WaitForStatus(status, timeSpan);
}
catch (System.ServiceProcess.TimeoutException) { }
catch (InvalidOperationException)
{
Task.Delay(timeSpan).Wait(); // force-wait
}
return this.CheckStatus(status);
});
}
示例15: RunServer
/// <summary>
/// Change the service status
/// </summary>
/// <param name="sc"></param>
/// <param name="newStatus"></param>
public static void RunServer(ServiceController sc, ServiceControllerStatus newStatus)
{
try
{
if (sc.Status == newStatus)
return;
// TODO: Need better waiting mechanism (ideally show a progress bar here...)
// for now wait 30 seconds and confirm the new status afterward.
var waitAmount = new TimeSpan(0, 0, 30);
switch (newStatus)
{
case ServiceControllerStatus.Running:
//Status("Starting server, please wait...");
sc.Start();
sc.WaitForStatus(newStatus, waitAmount);
break;
case ServiceControllerStatus.Stopped:
//Status("Stopping server, please wait...");
sc.Stop();
sc.WaitForStatus(newStatus, waitAmount);
break;
default:
throw new Exception("Unsupported action = " + newStatus.ToString());
}
if (sc.Status != newStatus)
throw new ApplicationException("Service is not " + newStatus);
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
throw;
}
}