本文整理汇总了C#中System.ServiceProcess.ServiceController.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# ServiceController.Dispose方法的具体用法?C# ServiceController.Dispose怎么用?C# ServiceController.Dispose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.ServiceProcess.ServiceController
的用法示例。
在下文中一共展示了ServiceController.Dispose方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Running
/// <summary>
/// Returns a flag that highlights whether the
/// window service with a particular name is
/// running.
/// </summary>
/// <param name="name">Name of the windows service
/// to check.</param>
/// <returns>True if running, otherwise false.</returns>
private static bool Running(string serviceName)
{
var service = new ServiceController(serviceName);
var result = (service.Status == ServiceControllerStatus.Running);
service.Dispose();
return result;
}
示例2: Running
/// <summary>
/// Checks to see if the Mongo DB service is currently running.
/// </summary>
/// <returns>True if currently running, otherwise false.</returns>
public static bool Running()
{
var service = new ServiceController(MongoServiceName);
var result = (service.Status == ServiceControllerStatus.Running);
service.Dispose();
return result;
}
示例3: Main
public static void Main(string[] args)
{
Eager.Initalize();
var service = new ServiceController("fogservice");
const string logName = "Update Helper";
Log.Entry(logName, "Shutting down service...");
//Stop the service
if (service.Status == ServiceControllerStatus.Running)
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped);
Log.Entry(logName, "Killing remaining FOG processes...");
if (Process.GetProcessesByName("FOGService").Length > 0)
foreach (var process in Process.GetProcessesByName("FOGService"))
process.Kill();
Log.Entry(logName, "Applying MSI...");
ApplyUpdates();
//Start the service
Log.Entry(logName, "Starting service...");
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running);
service.Dispose();
if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + @"\updating.info"))
File.Delete(AppDomain.CurrentDomain.BaseDirectory + @"\updating.info");
}
示例4: BasicInstallTest
public void BasicInstallTest()
{
ServiceController controler;
if (!ShonInstaller.HasServiceInstallationRigths())
Assert.Ignore("Need admin rigthts for this test");
try
{
ShonInstaller.InstallService("toto");
// sleep to ensure status is propagated
var watch = new Stopwatch();
while (watch.ElapsedMilliseconds < 1000)
{
Thread.Sleep(100);
controler = new ServiceController("toto");
try
{
if (ServiceControllerStatus.Stopped == controler.Status)
{
break;
}
}
catch
{
}
finally
{
controler.Dispose();
}
}
controler = new ServiceController("toto");
try
{
Assert.AreEqual(ServiceControllerStatus.Stopped, controler.Status);
}
catch
{
Assert.Fail("Service not found, installation failed");
}
finally
{
controler.Dispose();
}
}
finally
{
ShonInstaller.UninstallService("toto");
}
Thread.Sleep(100);
try
{
Assert.AreEqual(ServiceControllerStatus.Stopped, controler.Status);
Assert.Fail("Service found,should have been uninstalled");
}
catch
{
}
}
示例5: MyServiceInstaller_AfterInstall
void MyServiceInstaller_AfterInstall(object sender, InstallEventArgs e)
{
ServiceController controller = new ServiceController(this.ServiceName);
try {
controller.Start();
} finally {
controller.Dispose();
}
}
示例6: Start
/// <summary>
/// Starts the Mongo.
/// </summary>
public static void Start()
{
var service = new ServiceController(MongoServiceName);
if (service.Status == ServiceControllerStatus.Running)
{
service.Dispose();
return;
}
try
{
TimeSpan timeout = TimeSpan.FromMilliseconds(MongoTimeout);
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
}
finally
{
service.Dispose();
}
}
示例7: StopService
void StopService()
{
System.ServiceProcess.ServiceController serverContorler = new ServiceController(this.aidServiceInstaller.ServiceName);
if (serverContorler != null)
{
if (serverContorler.CanStop)
{
serverContorler.Stop();
serverContorler.Dispose();
}
}
}
示例8: StartService
void StartService()
{
System.ServiceProcess.ServiceController serverContorler = new ServiceController(this.aidServiceInstaller.ServiceName);
if (serverContorler != null)
{
if (serverContorler.Status != ServiceControllerStatus.Running)
{
serverContorler.Start();
serverContorler.Dispose();
}
}
}
示例9: Start
/// <summary>
/// Starts a windows service.
/// </summary>
/// <param name="name">Name of the windows service.</param>
private static void Start(string serviceName)
{
var service = new ServiceController(serviceName);
try
{
TimeSpan timeout = TimeSpan.FromMilliseconds(Timeout);
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
}
finally
{
service.Dispose();
}
}
示例10: 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);
}
示例11: DoesServiceExist
public static bool DoesServiceExist(string serviceName)
{
ServiceController service = null;
string status = "";
try {
service = new ServiceController(serviceName);
status = service.Status.ToString();
return true;
}
catch (InvalidOperationException)
{
return false;
}
finally
{
if (service != null)
{
service.Dispose();
}
}
}
示例12: StartServiceIfNeeded
/// <summary>
/// Starts service if it's stopped. Returns True if service was started.
/// </summary>
public bool StartServiceIfNeeded(string serviceName, int timeout = 30)
{
Ensure.That(() => serviceName).IsNotNullOrEmpty();
if (ServiceIsRunning(serviceName))
return false;
ServiceController sc = null;
try
{
sc = new ServiceController(serviceName);
if (sc.Status == ServiceControllerStatus.Stopped)
{
WriteStatus($"Starting {serviceName}");
sc.Start();
sc.WaitForStatus(ServiceControllerStatus.Running);
}
if (sc.Status == ServiceControllerStatus.StartPending)
{
WriteStatus($"Starting {serviceName}");
sc.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(timeout));
}
}
catch (Exception ex)
{
WriteException(ex);
WriteStatus($"Failed to start service {serviceName} more information exists in the activity log.");
return false;
}
finally
{
if (sc != null)
sc.Dispose();
}
WriteStatus($"Service {serviceName} has started.");
return true;
}
示例13: RestartService
public static void RestartService(string serviceName, int timeoutMilliseconds)
{
ServiceController service = null;
try
{
service = new ServiceController(serviceName);
int millisec1 = Environment.TickCount;
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
// count the rest of the timeout
int millisec2 = Environment.TickCount;
timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds - (millisec2 - millisec1));
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
}
catch
{
// ...
}
finally
{
if (service != null)
{
service.Dispose();
}
}
}
示例14: Lock_XI_Devices
//Disable all Xinput controllers presented by SCP
public void Lock_XI_Devices()
{
string strClassGUID = "{d61ca365-5af4-4486-998b-9db4734c6ca3}";
Guid ClassGUID = new Guid(strClassGUID);
//find all "Xbox 360 Controller for Windows" pads and disable them
ManagementObjectSearcher objSearcher = new ManagementObjectSearcher("Select * from Win32_PnPSignedDriver WHERE ClassGuid = '" + strClassGUID + "' AND DeviceName = 'Xbox 360 Controller for Windows'");
ManagementObjectCollection objCollection = objSearcher.Get();
List<string> objdeviceids = new List<string>();
foreach (ManagementObject cobj in objCollection)
{
string Local = (string)cobj["Location"];
//Dealing with X360 controllers outside of
//SCP is untested and might cause a crash
if (Local.Contains("SCP Virtual X360 Bus"))
objdeviceids.Add((string)cobj["DeviceID"]);
}
//Stop Service to allow us to disable needed X360 controllers
//Stopping the service effectivly unplugs the emulated X360
//controller, but we can still disable and re-enable it
//If we do this while the service is running, the program will
//crash
ServiceController sc = new ServiceController("SCP DSx Service");
try
{
sc.Stop();
sc.WaitForStatus(ServiceControllerStatus.Stopped);
foreach (string objdeviceid in objdeviceids)
{
if (objdeviceid != "")
{
DeviceHelper.SetDeviceEnabled(ClassGUID, objdeviceid, false, false);
lockedDevices.Add(new DeviceID(ClassGUID, objdeviceid));
}
}
//Restart service
sc.Start();
sc.WaitForStatus(ServiceControllerStatus.Running);
}
finally
{
sc.Dispose();
}
}
示例15: SetServiceRecoveryOptions
public static void SetServiceRecoveryOptions(
string serviceName, [NotNull] ServiceRecoveryOptions recoveryOptions)
{
if (recoveryOptions == null)
throw new ArgumentNullException("recoveryOptions");
bool requiresShutdownPriveleges =
recoveryOptions.FirstFailureAction == ServiceRecoveryAction.RestartComputer ||
recoveryOptions.SecondFailureAction == ServiceRecoveryAction.RestartComputer ||
recoveryOptions.SubsequentFailureAction == ServiceRecoveryAction.RestartComputer;
if (requiresShutdownPriveleges)
GrantShutdownPrivileges();
int actionCount = 3;
var restartServiceAfter = (uint)TimeSpan.FromMinutes(
recoveryOptions.RestartServiceWaitMinutes).TotalMilliseconds;
IntPtr failureActionsPointer = IntPtr.Zero;
IntPtr actionPointer = IntPtr.Zero;
ServiceController controller = null;
try
{
// Open the service
controller = new ServiceController(serviceName);
// Set up the failure actions
var failureActions = new SERVICE_FAILURE_ACTIONS();
failureActions.dwResetPeriod = (int)TimeSpan.FromDays(recoveryOptions.ResetFailureCountWaitDays).TotalSeconds;
failureActions.cActions = (uint)actionCount;
failureActions.lpRebootMsg = recoveryOptions.RestartSystemMessage;
// allocate memory for the individual actions
actionPointer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(SC_ACTION))*actionCount);
ServiceRecoveryAction[] actions = {
recoveryOptions.FirstFailureAction,
recoveryOptions.SecondFailureAction,
recoveryOptions.SubsequentFailureAction
};
for (int i = 0; i < actions.Length; i++)
{
ServiceRecoveryAction action = actions[i];
SC_ACTION scAction = GetScAction(action, restartServiceAfter);
Marshal.StructureToPtr(scAction, (IntPtr)((Int64)actionPointer + (Marshal.SizeOf(typeof(SC_ACTION)))*i), false);
}
failureActions.lpsaActions = actionPointer;
string command = recoveryOptions.RunProgramCommand;
if (command != null)
failureActions.lpCommand = command;
failureActionsPointer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(SERVICE_FAILURE_ACTIONS)));
Marshal.StructureToPtr(failureActions, failureActionsPointer, false);
// Make the change
bool success = ChangeServiceConfig2(controller.ServiceHandle.DangerousGetHandle(),
SERVICE_CONFIG_FAILURE_ACTIONS,
failureActionsPointer);
// Check that the change occurred
if (!success)
throw new Win32Exception(Marshal.GetLastWin32Error(), "Unable to change the Service configuration.");
}
finally
{
if (failureActionsPointer != IntPtr.Zero)
Marshal.FreeHGlobal(failureActionsPointer);
if (actionPointer != IntPtr.Zero)
Marshal.FreeHGlobal(actionPointer);
if (controller != null)
{
controller.Dispose();
}
//log.Debug(m => m("Done setting service recovery options."));
}
}