本文整理汇总了C#中ServiceControllerStatus.ToString方法的典型用法代码示例。如果您正苦于以下问题:C# ServiceControllerStatus.ToString方法的具体用法?C# ServiceControllerStatus.ToString怎么用?C# ServiceControllerStatus.ToString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ServiceControllerStatus
的用法示例。
在下文中一共展示了ServiceControllerStatus.ToString方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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;
}
}
示例2: HandleServiceState
private void HandleServiceState(ServiceControllerStatus _status)
{
switch (_status)
{
case ServiceControllerStatus.Stopped:
btnStartStopService.Content = "Start";
lblServiceState.Content = "Service Stopped";
lblServiceState.Foreground = System.Windows.Media.Brushes.Red;
break;
case ServiceControllerStatus.Running:
btnStartStopService.Content = "Stop";
lblServiceState.Content = "Service Started";
lblServiceState.Foreground = System.Windows.Media.Brushes.Green;
break;
case ServiceControllerStatus.StartPending:
btnStartStopService.Content = "Stop";
lblServiceState.Content = "Service Starting";
lblServiceState.Foreground = System.Windows.Media.Brushes.Teal;
break;
default:
lblServiceState.Foreground = System.Windows.Media.Brushes.Teal;
lblServiceState.Content = "Service " + _status.ToString();
break;
}
}
示例3: UpdateServiceUI
private void UpdateServiceUI()
{
if (mediaServer != null && standAloneMode == true)
{
serverStatus1.Text = "Serving " + mediaServer.TotalFileCount + " Files in " + mediaServer.TotalDirectoryCount + " Directories";
serverStatus2.Text = "";
UpdateDirectoriesUI();
return;
}
if (serviceController == null)
{
try
{
serviceController = new ServiceController("AV Media Server");
serviceStatus = serviceController.Status;
}
catch (System.InvalidOperationException)
{
serverStatus1.Text = "Media Server Service Not Installed";
serverStatus2.Text = "Use \"Standalone Mode\" for autonomous operation";
serviceController = null;
serviceStatus = ServiceControllerStatus.Stopped;
return;
}
}
ServiceController serviceController2 = new ServiceController("AV Media Server");
serviceStatus = serviceController2.Status;
switch (serviceStatus)
{
case ServiceControllerStatus.Running:
if (mediaServer == null)
{
serverStatus1.Text = "Connecting...";
serverStatus2.Text = "";
Connect();
}
else
{
serverStatus1.Text = "Media Server Serving "+mediaServer.TotalFileCount+" Files in "+mediaServer.TotalDirectoryCount+" Directories";
serverStatus2.Text = "";
}
break;
case ServiceControllerStatus.Stopped:
serverStatus1.Text = "Media Server Service is Stopped";
serverStatus2.Text = "";
break;
default:
serverStatus1.Text = "Media Server Service is " + serviceStatus.ToString();
serverStatus2.Text = "";
break;
}
serviceController2.Close();
serviceController2.Dispose();
serviceController2 = null;
UpdateDirectoriesUI();
}
示例4: GetDisplayName
public string GetDisplayName(ServiceControllerStatus scs)
{
//Stopped
string name = "已停止";
if (scs.ToString().Equals("Running"))
{
name = "已运行";
}
return name;
}
示例5: setServiceStatus
private void setServiceStatus(ServiceControllerStatus status)
{
if (serviceController.Status.Equals(status)) //already in the wanted status
{
OnProgress(processIdx + 1);
}
else
{
try
{
TimeSpan timeoutSec = TimeSpan.FromSeconds(timeout);
bool tryAgain = false;
int tryCount = 0;
do
{
try
{
if (status.Equals(ServiceControllerStatus.Running))
serviceController.Start(); //TODO: support arguments
else
serviceController.Stop();
}
catch (InvalidOperationException e)
{
//in some cases, the service status might be changed although the exception is thrown
Thread.Sleep(TimeSpan.FromSeconds(5)); //wait a little bit time to try again
serviceController.Refresh();
if (serviceController.Status.Equals(status)) //equal
tryAgain = false;
else if (tryCount < TOTAL_TRY) //not equal, not reach total
tryAgain = true;
else //when not equal to status, AND reach total try
throw e;
tryCount++;
}
} while (tryAgain);
serviceController.WaitForStatus(status, timeoutSec);
Console.WriteLine(serviceController.Status.ToString());
//check the status to make sure it is started properly
if (serviceController.Status.Equals(status))
{
OnProgress(processIdx + 1);
}
else
{
throw new InvalidOperationException(serviceController.ServiceName + " cannot be changed to status " + status.ToString());
}
}
//TODO: there should be configuration for the app
//when error occurs to a service, should it stop or continue to next service?
catch (InvalidOperationException e)
{
OnError(e);
}
catch (System.ServiceProcess.TimeoutException e)
{
OnError(e);
}
}
}