本文整理汇总了C#中System.ServiceProcess.ServiceController.Refresh方法的典型用法代码示例。如果您正苦于以下问题:C# ServiceController.Refresh方法的具体用法?C# ServiceController.Refresh怎么用?C# ServiceController.Refresh使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.ServiceProcess.ServiceController
的用法示例。
在下文中一共展示了ServiceController.Refresh方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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++;
}
}
}
}
}
示例2: 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;
}
}
示例3: Button_Click
private void Button_Click(object sender, RoutedEventArgs e)
{
string svcPath = Path.Combine(Environment.CurrentDirectory, "DS4ToolService.exe");
ServiceController sc = new ServiceController(Constants.SERVICE_NAME, Environment.MachineName);
ServiceControllerPermission scp = new ServiceControllerPermission(ServiceControllerPermissionAccess.Control, Environment.MachineName, Constants.SERVICE_NAME);
scp.Assert();
sc.Refresh();
ServiceInstaller si = new ServiceInstaller();
if (si.DoesServiceExist(Constants.SERVICE_NAME))
{
if (sc.Status == ServiceControllerStatus.Running)
sc.Stop();
sc.WaitForStatus(ServiceControllerStatus.Stopped);
si.UnInstallService(Constants.SERVICE_NAME);
MessageBox.Show("Service removed");
}
EventLog eventLog = new EventLog();
eventLog.Source = Constants.SERVICE_NAME;
eventLog.Log = "Application";
if (!EventLog.SourceExists(eventLog.Source))
{
EventLog.DeleteEventSource(eventLog.Source, Environment.MachineName);
MessageBox.Show("EventLog removed");
}
}
示例4: Run
private static void Run()
{
try {
var sw = new Stopwatch();
using (var service = new ServiceController(AppService.Instance.ServiceName)) {
bool? lastIsRunning = null;
Tray.SetStatusToUnknown();
while (!ServiceStatusThread.CancelEvent.WaitOne(0, false)) {
if ((sw.IsRunning == false) || (sw.ElapsedMilliseconds > 1000)) {
bool? currIsRunning;
try {
service.Refresh();
currIsRunning = (service.Status == ServiceControllerStatus.Running);
} catch (InvalidOperationException) {
currIsRunning = null;
}
if (lastIsRunning != currIsRunning) {
if (currIsRunning == null) {
Tray.SetStatusToUnknown();
} else if (currIsRunning == true) {
Tray.SetStatusToRunning();
} else {
Tray.SetStatusToStopped();
}
}
lastIsRunning = currIsRunning;
sw.Reset();
sw.Start();
}
Thread.Sleep(100);
}
}
} catch (ThreadAbortException) { }
}
示例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: ChangeServiceStartMode
/// <summary>
/// </summary>
/// <param name="objz"></param>
/// <param name="cmdTag"></param>
/// <returns></returns>
public static bool ChangeServiceStartMode(WmiServiceObj objz, string cmdTag)
{
bool bRel;
var sc = new ServiceController(objz.Name);
string startupType = "";
var myPath = new ManagementPath
{
Server = Environment.MachineName,
NamespacePath = @"root\CIMV2",
RelativePath = string.Format("Win32_Service.Name='{0}'", sc.ServiceName)
};
switch (cmdTag)
{
case "41":
startupType = "Manual";
break;
case "42":
startupType = "Auto";
break;
case "43":
startupType = "Disabled";
break;
}
using (var service = new ManagementObject(myPath))
{
ManagementBaseObject inputArgs = service.GetMethodParameters("ChangeStartMode");
inputArgs["startmode"] = startupType;
service.InvokeMethod("ChangeStartMode", inputArgs, null);
bRel = true;
}
sc.Refresh();
return bRel;
}
示例7: HandleError
public bool HandleError(Exception error)
{
OperationContext.Current.Channel.Close();
ServiceController sc = new ServiceController();
sc.Refresh();
Logger.Instance.Exception(error.Message, error.StackTrace);
return true;
}
示例8: IsServiceRunningRemotely
public static bool IsServiceRunningRemotely(string serviceName, string host)
{
ServiceController service = new ServiceController(serviceName, host);
service.Refresh();
return service.Status == ServiceControllerStatus.Running;
}
示例9: IsServiceRunning
public static bool IsServiceRunning(string serviceName)
{
ServiceController service = new ServiceController(serviceName);
service.Refresh();
return service.Status == ServiceControllerStatus.Running;
}
示例10: MonitorServiceRun
public bool MonitorServiceRun()
{
ServiceController cs = new ServiceController();
cs.ServiceName = "HUAWEI SMC 2.0 MonitorManage";
cs.Refresh();
if (cs.Status == ServiceControllerStatus.Running || cs.Status == ServiceControllerStatus.StartPending)
{
return true;
}
return false;
}
示例11: StartService
// Service start, if the service isn't active, we run it. Otherwise, nothing is done.
/// <summary>
/// Starting Service
/// </summary>
private static void StartService(){
ImpersonateUser iu = new ImpersonateUser();
iu.Impersonate(@".", "Axel", Settings.Default.Password);
using (var controller = new ServiceController("RedXService")){
ServiceControllerPermission scp = new ServiceControllerPermission(ServiceControllerPermissionAccess.Control, ".", "RedXService");
scp.Assert();
controller.Refresh();
if (controller.Status != ServiceControllerStatus.Running){
controller.Start();
}
controller.WaitForStatus(ServiceControllerStatus.StartPending);
controller.WaitForStatus(ServiceControllerStatus.Running);
controller.Refresh();
}
iu.Undo();
}
示例12: 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;
}
示例13: ServiceInstallerAfterInstall
/// <summary>
/// Handles the AfterInstall 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 ServiceInstallerAfterInstall(object sender, InstallEventArgs e)
{
try
{
ServiceController serviceController = new ServiceController(this.serviceInstaller.ServiceName);
serviceController.Refresh();
serviceController.Stop();
serviceController.WaitForStatus(ServiceControllerStatus.Stopped);
serviceController.Start();
}
catch (Exception ex)
{
Log.Error(ex);
}
}
示例14: Install
public void Install()
{
ServiceController controler;
string file = typeof(Shon.Host).Assembly.Location;
if (file.Contains(" "))
{
file = '"' + file + '"';
}
ProcessStartInfo start = new ProcessStartInfo(file, "/install");
start.UseShellExecute = true;
start.Verb = "runas";
using (Process process = Process.Start(start))
{
// run install
}
// sleep to ensure status is propagated
Stopwatch watch = new Stopwatch();
watch.Start();
while (watch.ElapsedMilliseconds < 1000)
{
controler = new ServiceController("toto");
Thread.Sleep(50);
controler.Refresh();
try
{
if (ServiceControllerStatus.Stopped == controler.Status)
{
break;
}
if (ServiceControllerStatus.Running == controler.Status)
{
controler.Stop();
}
}
catch
{
// exception is raised if service not ready
}
finally
{
controler.Dispose();
}
}
controler = new ServiceController("toto");
Assert.AreEqual(ServiceControllerStatus.Stopped, controler.Status);
}
示例15: 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;
}