PDA

View Full Version : سوال: کار کردن با services



malaki.davud
چهارشنبه 17 مهر 1392, 14:52 عصر
با سلام
کسی نحوه ی کار کردن با قسمت services در ویندوز می دونه چطوره ؟
مثلا سرویس print spooler در هر 2 ساعت restart کنم با تشکر
111686

malaki.davud
یک شنبه 21 مهر 1392, 09:56 صبح
کسی راه حلی نداره
با تشکر

malaki.davud
جمعه 24 آبان 1392, 16:37 عصر
Start, Stop and Restart Windows Service [C#]

This example shows how to start, stop and restart a windows service programmatically in C#.

Start service
The following method tries to start a service specified by a service name. Then it waits until the service is running or a timeout occurs.

[C#]
public static void StartService(string serviceName, int timeoutMilliseconds)
{
ServiceController service = new ServiceController(serviceName);
try
{
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);

service.Start();
service.WaitForStatus(ServiceControllerStatus.Runn ing, timeout);
}
catch
{
// ...
}
}

Stop service
The following method tries to stop the specified service and it waits until the service is stopped or a timeout occurs.

[C#]
public static void StopService(string serviceName, int timeoutMilliseconds)
{
ServiceController service = new ServiceController(serviceName);
try
{
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);

service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stop ped, timeout);
}
catch
{
// ...
}
}

Restart service
This method combinates both previous methods. It tries to stop the service (and waits until it's stopped) then it begins to start the service (and waits until the service is running). The specified timeout is used for both operations together.

[C#]
public static void RestartService(string serviceName, int timeoutMilliseconds)
{
ServiceController service = new ServiceController(serviceName);
try
{
int millisec1 = Environment.TickCount;
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);

service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stop ped, timeout);

// count the rest of the timeout
int millisec2 = Environment.TickCount;
timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds - (millisec2-millisec1));

service.Start();
service.WaitForStatus(ServiceControllerStatus.Runn ing, timeout);
}
catch
{
// ...
}
}

malaki.davud
جمعه 24 آبان 1392, 16:38 عصر
لینک مطلب بالا
http://www.csharp-examples.net/restart-windows-service/