- 전체
- Native Apps
- WinJS App
- C# Apps
- XAML
- VB.net
- VisualC.net
- C++
- MFC
- visual studio mobile app dev
- Azure ms cloud service
- Asp.net
- 인공지능 (AI)
- wpf
- UWP
- MAUI
- asp.net
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.Running, 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.Stopped, 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.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 { // ... } }
본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
댓글 0
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 수 |
|---|---|---|---|---|
| 8 |
[Windows Apps][MFC] HTTP SPY : HTTP 스파이
| 졸리운_곰 | 2023.11.29 | 185 |
| 7 |
[Windows Apps][MFC] 2D LUA Based Robot Simulator : 2D LUA 기반 로봇 시뮬레이터
| 졸리운_곰 | 2023.11.29 | 183 |
| 6 |
Runtime Object Editor
| 졸리운_곰 | 2017.07.01 | 539 |
| 5 |
** OpenGL 좌표계에서 D3D좌표계로... **
| 가을의곰 | 2017.06.10 | 267 |
| 4 |
SQLite on Visual Studio with NuGet and Easy Instructions
| 졸리운_곰 | 2016.08.11 | 329 |
| 3 | Using SQLite in a Native Visual C++ Application | 졸리운_곰 | 2016.08.11 | 276 |
| 2 |
[학습정리] 민대규님의 학습정리 프로그램 "대굴이의 학습정리"
| 졸리운_곰 | 2015.05.11 | 445 |
| 1 |
[일정관리] 민대규님 제작 "대굴이의 일정관리"
| 졸리운_곰 | 2015.05.11 | 1841 |

