本文整理汇总了C#中System.ServiceProcess.ServiceController.Pause方法的典型用法代码示例。如果您正苦于以下问题:C# ServiceController.Pause方法的具体用法?C# ServiceController.Pause怎么用?C# ServiceController.Pause使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.ServiceProcess.ServiceController
的用法示例。
在下文中一共展示了ServiceController.Pause方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: btnPauseContinue_Click
private void btnPauseContinue_Click(object sender, EventArgs e)
{
string serviceName = "EmailService";
try
{
ServiceController serviceController = new ServiceController(serviceName);
if (serviceController.CanPauseAndContinue)
{
if (serviceController.Status == ServiceControllerStatus.Running)
{
serviceController.Pause();
lblLog.Text = "服务已暂停";
}
else if (serviceController.Status == ServiceControllerStatus.Paused)
{
serviceController.Continue();
lblLog.Text = "服务已继续";
}
else
{
lblLog.Text = "服务未处于暂停和启动状态";
}
}
else
{
lblLog.Text = "服务不能暂停";
}
}
catch (Exception ex)
{
_log.Fatal(string.Format("未找到服务:{0} !", serviceName, ex.Message));
}
}
示例2: btnPauseContinue_Click
private void btnPauseContinue_Click(object sender, EventArgs e)
{
try
{
ServiceController serviceController = new ServiceController("ServiceShaka");
if (serviceController.CanPauseAndContinue)
{
if (serviceController.Status == ServiceControllerStatus.Running)
{
serviceController.Pause();
lblLog.Text = "服务已暂停";
}
else if (serviceController.Status == ServiceControllerStatus.Paused)
{
serviceController.Continue();
lblLog.Text = "服务已继续";
}
else
{
lblLog.Text = "服务未处于暂停和启动状态";
}
}
else
{
lblLog.Text = "服务不能暂停";
}
}
catch (Exception ex)
{
}
}
示例3: DoPauseService
internal bool DoPauseService(ServiceController serviceController)
{
Exception exception = null;
bool flag = false;
try
{
serviceController.Pause();
}
catch (Win32Exception win32Exception1)
{
Win32Exception win32Exception = win32Exception1;
if (0x426 == win32Exception.NativeErrorCode)
{
flag = true;
}
exception = win32Exception;
}
catch (InvalidOperationException invalidOperationException1)
{
InvalidOperationException invalidOperationException = invalidOperationException1;
Win32Exception innerException = invalidOperationException.InnerException as Win32Exception;
if (innerException != null && 0x426 == innerException.NativeErrorCode)
{
flag = true;
}
exception = invalidOperationException;
}
if (exception == null)
{
if (this.DoWaitForStatus(serviceController, ServiceControllerStatus.Paused, ServiceControllerStatus.PausePending, ServiceResources.SuspendingService, "SuspendServiceFailed", ServiceResources.SuspendServiceFailed))
{
return true;
}
else
{
return false;
}
}
else
{
if (!flag)
{
if (!serviceController.CanPauseAndContinue)
{
base.WriteNonTerminatingError(serviceController, exception, "CouldNotSuspendServiceNotSupported", ServiceResources.CouldNotSuspendServiceNotSupported, ErrorCategory.CloseError);
}
}
else
{
base.WriteNonTerminatingError(serviceController, exception, "CouldNotSuspendServiceNotRunning", ServiceResources.CouldNotSuspendServiceNotRunning, ErrorCategory.CloseError);
}
base.WriteNonTerminatingError(serviceController, exception, "CouldNotSuspendService", ServiceResources.CouldNotSuspendService, ErrorCategory.CloseError);
return false;
}
}
示例4: btnPause_Click
private void btnPause_Click(object sender, RoutedEventArgs e)
{
ServiceController serviceController = new ServiceController("ApolloOaService");
if (serviceController.CanPauseAndContinue)
{
if (serviceController.Status == ServiceControllerStatus.Running)
{
serviceController.Pause();
lblMessages.Content = "服务已暂停.";
}
else if (serviceController.Status == ServiceControllerStatus.Paused)
{
serviceController.Continue();
lblMessages.Content = "服务已恢复.";
}
}
}
示例5: PauseService
public static void PauseService(string serviceName, int timeoutMilliseconds)
{
ServiceController service = new ServiceController(serviceName);
try
{
if (service.Status == ServiceControllerStatus.Running)
{
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
service.Pause();
service.WaitForStatus(ServiceControllerStatus.Paused, timeout);
}
}
catch
{
throw;
}
}
示例6: btnPauseContinue_Click
private void btnPauseContinue_Click(object sender, RoutedEventArgs e)
{
ServiceController serviceController = new ServiceController("ServiceHr");
if (serviceController.CanPauseAndContinue)
{
if (serviceController.Status == ServiceControllerStatus.Running)
{
serviceController.Pause();
lblStatus.Text = "服务已暂停";
}
else if (serviceController.Status == ServiceControllerStatus.Paused)
{
serviceController.Continue();
lblStatus.Text = "服务已继续";
}
else
{
lblStatus.Text = "服务未处于暂停和启动状态";
}
}
else
lblStatus.Text = "服务不能暂停";
}
示例7: Pause_Service_OperationNotValid
public void Pause_Service_OperationNotValid ()
{
if (RunningOnUnix)
Assert.Ignore ("Running on Unix.");
ServiceController sc1 = new ServiceController ("SamSs", ".");
ServiceController sc2 = new ServiceController ("SamSs", ".");
Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#A1");
Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#A2");
try {
sc1.Pause ();
Assert.Fail ("#B1");
} catch (InvalidOperationException ex) {
// Cannot pause SamSs service on computer '.'
Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
Assert.IsNotNull (ex.Message, "#B3");
Assert.IsTrue (ex.Message.IndexOf ("SamSs") != -1, "#B4");
Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#B5");
Assert.IsNotNull (ex.InnerException, "#B6");
// The requested control is not valid for this service
Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#B7");
Win32Exception win32Error = (Win32Exception) ex.InnerException;
//Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#B8");
Assert.IsNotNull (win32Error.Message, "#B9");
Assert.AreEqual (1052, win32Error.NativeErrorCode, "#B10");
Assert.IsNull (win32Error.InnerException, "#B11");
}
Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#C1");
Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#C2");
}
示例8: Pause_Service_Disabled
public void Pause_Service_Disabled ()
{
if (RunningOnUnix)
Assert.Ignore ("Running on Unix.");
ServiceController sc1 = new ServiceController ("NetDDE", ".");
ServiceController sc2 = new ServiceController ("NetDDE", ".");
Assert.AreEqual (ServiceControllerStatus.Stopped, sc1.Status, "#A1");
Assert.AreEqual (ServiceControllerStatus.Stopped, sc2.Status, "#A2");
try {
sc1.Pause ();
Assert.Fail ("#B1");
} catch (InvalidOperationException ex) {
// Cannot pause NetDDE service on computer '.'
Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
Assert.IsNotNull (ex.Message, "#B3");
Assert.IsTrue (ex.Message.IndexOf ("NetDDE") != -1, "#B4");
Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#B5");
Assert.IsNotNull (ex.InnerException, "#B6");
// The service has not been started
Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#B7");
Win32Exception win32Error = (Win32Exception) ex.InnerException;
//Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#B8");
Assert.IsNotNull (win32Error.Message, "#B9");
Assert.AreEqual (1062, win32Error.NativeErrorCode, "#B10");
Assert.IsNull (win32Error.InnerException, "#B11");
}
Assert.AreEqual (ServiceControllerStatus.Stopped, sc1.Status, "#C1");
Assert.AreEqual (ServiceControllerStatus.Stopped, sc2.Status, "#C2");
}
示例9: Pause_Service_DoesNotExist
public void Pause_Service_DoesNotExist ()
{
if (RunningOnUnix)
Assert.Ignore ("Running on Unix.");
ServiceController sc = new ServiceController ("doesnotexist", ".");
try {
sc.Pause ();
Assert.Fail ("#1");
} catch (InvalidOperationException ex) {
// Cannot open doesnotexist service on computer '.'
Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
Assert.IsNotNull (ex.Message, "#3");
Assert.IsTrue (ex.Message.IndexOf ("doesnotexist") != -1, "#4");
Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#5");
Assert.IsNotNull (ex.InnerException, "#6");
// The filename, directory name, or volume label is incorrect
Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#7");
Win32Exception win32Error = (Win32Exception) ex.InnerException;
//Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#8");
Assert.IsNotNull (win32Error.Message, "#9");
Assert.AreEqual (1060, win32Error.NativeErrorCode, "#10");
Assert.IsNull (win32Error.InnerException, "#11");
}
}
示例10: ExecuteCommand_Service_PausePending
public void ExecuteCommand_Service_PausePending ()
{
if (RunningOnUnix)
Assert.Ignore ("Running on Unix.");
ServiceController sc = new ServiceController ("Schedule", ".");
Assert.AreEqual (ServiceControllerStatus.Running, sc.Status, "#A");
sc.Pause ();
try {
sc.ExecuteCommand (128);
try {
sc.ExecuteCommand (127);
Assert.Fail ("#B1");
} catch (InvalidOperationException ex) {
// Cannot control Schedule service on computer '.'
Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
Assert.IsNotNull (ex.Message, "#B3");
Assert.IsTrue (ex.Message.IndexOf ("Schedule") != -1, "#B4");
Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#B5");
Assert.IsNotNull (ex.InnerException, "#B6");
// The parameter is incorrect
Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#B7");
Win32Exception win32Error = (Win32Exception) ex.InnerException;
//Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#B8");
Assert.IsNotNull (win32Error.Message, "#B9");
Assert.AreEqual (87, win32Error.NativeErrorCode, "#B10");
Assert.IsNull (win32Error.InnerException, "#B11");
}
} finally {
EnsureServiceIsRunning (sc);
}
Assert.AreEqual (ServiceControllerStatus.Running, sc.Status, "#C");
}
示例11: Pause_Machine_DoesNotExist
public void Pause_Machine_DoesNotExist ()
{
if (RunningOnUnix)
Assert.Ignore ("Running on Unix.");
ServiceController sc = new ServiceController ("Schedule",
"doesnotexist");
try {
sc.Pause ();
Assert.Fail ("#1");
} catch (InvalidOperationException ex) {
// Cannot open Service Control Manager on computer 'doesnotexist'.
// This operation might require other priviliges
Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
Assert.IsNotNull (ex.Message, "#3");
Assert.IsTrue (ex.Message.IndexOf ("'doesnotexist'") != -1, "#4");
Assert.IsNotNull (ex.InnerException, "#5");
// The RPC server is unavailable
Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#6");
Win32Exception win32Error = (Win32Exception) ex.InnerException;
//Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#7");
Assert.IsNotNull (win32Error.Message, "#8");
Assert.AreEqual (1722, win32Error.NativeErrorCode, "#9");
Assert.IsNull (win32Error.InnerException, "#10");
}
}
示例12: Constructor1
public void Constructor1 ()
{
if (RunningOnUnix)
Assert.Ignore ("Running on Unix.");
ServiceController sc = new ServiceController ();
try {
bool value = sc.CanPauseAndContinue;
Assert.Fail ("#A1: " + value.ToString ());
} catch (ArgumentException ex) {
// Service name contains invalid characters, is empty or is
// too long (max length = 80)
Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
Assert.IsNotNull (ex.Message, "#A3");
Assert.IsTrue (ex.Message.IndexOf (" ") != -1, "#A4");
Assert.IsTrue (ex.Message.IndexOf ("80") != -1, "#A5");
Assert.IsNull (ex.ParamName, "#A6");
Assert.IsNull (ex.InnerException, "#A7");
}
try {
bool value = sc.CanShutdown;
Assert.Fail ("#B1: " + value.ToString ());
} catch (ArgumentException ex) {
// Service name contains invalid characters, is empty or is
// too long (max length = 80)
Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
Assert.IsNotNull (ex.Message, "#B3");
Assert.IsTrue (ex.Message.IndexOf (" ") != -1, "#B4");
Assert.IsTrue (ex.Message.IndexOf ("80") != -1, "#B5");
Assert.IsNull (ex.ParamName, "#B6");
Assert.IsNull (ex.InnerException, "#B7");
}
try {
bool value = sc.CanStop;
Assert.Fail ("#C1: " + value.ToString ());
} catch (ArgumentException ex) {
// Service name contains invalid characters, is empty or is
// too long (max length = 80)
Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
Assert.IsNotNull (ex.Message, "#C3");
Assert.IsTrue (ex.Message.IndexOf (" ") != -1, "#C4");
Assert.IsTrue (ex.Message.IndexOf ("80") != -1, "#C5");
Assert.IsNull (ex.ParamName, "#C6");
Assert.IsNull (ex.InnerException, "#C7");
}
// closing the ServiceController does not result in exception
sc.Close ();
try {
sc.Continue ();
Assert.Fail ("#D1");
} catch (ArgumentException ex) {
// Service name contains invalid characters, is empty or is
// too long (max length = 80)
Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#D2");
Assert.IsNotNull (ex.Message, "#D3");
Assert.IsTrue (ex.Message.IndexOf (" ") != -1, "#D4");
Assert.IsTrue (ex.Message.IndexOf ("80") != -1, "#D5");
Assert.IsNull (ex.ParamName, "#D6");
Assert.IsNull (ex.InnerException, "#D7");
}
try {
Assert.Fail ("#E1: " + sc.DependentServices.Length);
} catch (ArgumentException ex) {
// Service name contains invalid characters, is empty or is
// too long (max length = 80)
Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#E2");
Assert.IsNotNull (ex.Message, "#E3");
Assert.IsTrue (ex.Message.IndexOf (" ") != -1, "#E4");
Assert.IsTrue (ex.Message.IndexOf ("80") != -1, "#E5");
Assert.IsNull (ex.ParamName, "#E6");
Assert.IsNull (ex.InnerException, "#E7");
}
Assert.IsNotNull (sc.DisplayName, "#F1");
Assert.AreEqual (string.Empty, sc.DisplayName, "#F2");
try {
sc.ExecuteCommand (0);
Assert.Fail ("#G1");
} catch (ArgumentException ex) {
// Service name contains invalid characters, is empty or is
// too long (max length = 80)
Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#G2");
Assert.IsNotNull (ex.Message, "#G3");
Assert.IsTrue (ex.Message.IndexOf (" ") != -1, "#G4");
Assert.IsTrue (ex.Message.IndexOf ("80") != -1, "#G5");
Assert.IsNull (ex.ParamName, "#G6");
Assert.IsNull (ex.InnerException, "#G7");
}
Assert.IsNotNull (sc.MachineName, "#H1");
Assert.AreEqual (".", sc.MachineName, "#H2");
//.........这里部分代码省略.........
示例13: CanPauseAndContinue_Service_Paused
public void CanPauseAndContinue_Service_Paused ()
{
if (RunningOnUnix)
Assert.Ignore ("Running on Unix.");
ServiceController sc1 = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
ServiceController sc2 = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#A1");
Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#A2");
Assert.IsTrue (sc1.CanPauseAndContinue, "#B1");
Assert.IsTrue (sc2.CanPauseAndContinue, "#B2");
sc1.Pause ();
try {
Assert.IsTrue (sc1.CanPauseAndContinue, "#C1");
Assert.IsTrue (sc2.CanPauseAndContinue, "#C2");
sc1.WaitForStatus (ServiceControllerStatus.Paused, new TimeSpan (0, 0, 5));
Assert.IsTrue (sc1.CanPauseAndContinue, "#D1");
Assert.IsTrue (sc2.CanPauseAndContinue, "#D2");
} finally {
EnsureServiceIsRunning (sc1);
sc2.Refresh ();
}
Assert.IsTrue (sc1.CanPauseAndContinue, "#E1");
Assert.IsTrue (sc2.CanPauseAndContinue, "#E2");
Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#F1");
Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#F2");
}
示例14: PauseService
/// <summary>
/// Pause the service with the given name and wait until the service status is paused.
/// If the service status is not paused after the given timeout then the service is considered not paused.
/// </summary>
/// <param name="serviceName">The name of the service</param>
/// <param name="timeout">The timeout.</param>
/// <returns>True if the service has been paused. Otherwise, false.</returns>
public static bool PauseService(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.Paused)
return true;
if (!timeoutEnabled && (c.Status == ServiceControllerStatus.Paused || c.Status == ServiceControllerStatus.PausePending))
return true;
c.Pause();
if (timeoutEnabled)
c.WaitForStatus(ServiceControllerStatus.Paused, timeout);
return true;
}
}
catch (Exception e)
{
Utils.Trace(e, "Unexpected error pausing service {0}.", serviceName);
return false;
}
}
示例15: Main
static void Main(string[] args)
{
ServiceController[] scServices;
scServices = ServiceController.GetServices();
foreach (ServiceController scTemp in scServices)
{
if (scTemp.ServiceName == "Simple Service")
{
// Display properties for the Simple Service sample
// from the ServiceBase example.
ServiceController sc = new ServiceController("Simple Service");
Console.WriteLine("Status = " + sc.Status);
Console.WriteLine("Can Pause and Continue = " + sc.CanPauseAndContinue);
Console.WriteLine("Can ShutDown = " + sc.CanShutdown);
Console.WriteLine("Can Stop = " + sc.CanStop);
if (sc.Status == ServiceControllerStatus.Stopped)
{
sc.Start();
while (sc.Status == ServiceControllerStatus.Stopped)
{
Thread.Sleep(1000);
sc.Refresh();
}
}
// Issue custom commands to the service
// enum SimpleServiceCustomCommands
// { StopWorker = 128, RestartWorker, CheckWorker };
sc.ExecuteCommand((int)SimpleServiceCustomCommands.StopWorker);
sc.ExecuteCommand((int)SimpleServiceCustomCommands.RestartWorker);
sc.Pause();
while (sc.Status != ServiceControllerStatus.Paused)
{
Thread.Sleep(1000);
sc.Refresh();
}
Console.WriteLine("Status = " + sc.Status);
sc.Continue();
while (sc.Status == ServiceControllerStatus.Paused)
{
Thread.Sleep(1000);
sc.Refresh();
}
Console.WriteLine("Status = " + sc.Status);
sc.Stop();
while (sc.Status != ServiceControllerStatus.Stopped)
{
Thread.Sleep(1000);
sc.Refresh();
}
Console.WriteLine("Status = " + sc.Status);
String[] argArray = new string[] { "ServiceController arg1", "ServiceController arg2" };
sc.Start(argArray);
while (sc.Status == ServiceControllerStatus.Stopped)
{
Thread.Sleep(1000);
sc.Refresh();
}
Console.WriteLine("Status = " + sc.Status);
// Display the event log entries for the custom commands
// and the start arguments.
EventLog el = new EventLog("Application");
EventLogEntryCollection elec = el.Entries;
foreach (EventLogEntry ele in elec)
{
if (ele.Source.IndexOf("SimpleService.OnCustomCommand") >= 0 |
ele.Source.IndexOf("SimpleService.Arguments") >= 0)
Console.WriteLine(ele.Message);
}
}
}
Console.ReadKey();
}