PDA

View Full Version : گفتگو: توضیح کد



User_Soual
جمعه 14 دی 1397, 01:41 صبح
با سلام به دوستان گرامی من یک قطعه کد داشتم معنی و کارشو نمیدونم از شما میخوام جزئی و بخش به بخش برام توضیحش بدید با تشکر خیلی مهمه

using System;
using System.Collections;
using System.Configuration.Install;
using System.Linq;
using System.ServiceProcess;

public class WindowsServiceAssemblyInstaller
{
private string _serviceName;
private Type _serviceType;

public WindowsServiceAssemblyInstaller(string serviceName , Type serviceType)
{
_serviceName = serviceName;
_serviceType = serviceType;
}



private bool IsInstalled()
{
return ServiceController.GetServices().Any(item => item.ServiceName == _serviceName);
}

private bool IsRunning()
{
if (!IsInstalled()) return false;
using (ServiceController controller =new ServiceController(_serviceName))
{
return (controller.Status == ServiceControllerStatus.Running);
}
}

private AssemblyInstaller GetInstaller()
{
AssemblyInstaller installer = new AssemblyInstaller(_serviceType.Assembly, null);
installer.UseNewContext = true;
return installer;
}

public void InstallService()
{
if (IsInstalled()) return;

try
{
using (AssemblyInstaller installer = GetInstaller())
{
IDictionary state = new Hashtable();
try
{
installer.Install(state);
installer.Commit(state);
}
catch
{
try
{
installer.Rollback(state);
}
catch
{
}

throw;
}
}
}
catch
{
throw;
}
}

public void UninstallService()
{
if (!IsInstalled()) return;
try
{
using (AssemblyInstaller installer = GetInstaller())
{
IDictionary state = new Hashtable();
try
{
installer.Uninstall(state);
}
catch
{
throw;
}
}
}
catch
{
throw;
}
}

public void StartService()
{
if (!IsInstalled()) return;

using (ServiceController controller =
new ServiceController(_serviceName))
{
try
{
if (controller.Status != ServiceControllerStatus.Running)
{
controller.Start();
controller.WaitForStatus(ServiceControllerStatus.R unning,
TimeSpan.FromSeconds(10));
}
}
catch
{
throw;
}
}
}

public void StopService()
{
if (!IsInstalled()) return;
using (ServiceController controller =
new ServiceController(_serviceName))
{
try
{
if (controller.Status != ServiceControllerStatus.Stopped)
{
controller.Stop();
controller.WaitForStatus(ServiceControllerStatus.S topped,
TimeSpan.FromSeconds(10));
}
}
catch
{
throw;
}
}
}
}

User_Soual
جمعه 14 دی 1397, 15:40 عصر
الو کسی جواب نمیده