6 Commits
93cd73c5e5
...
6f3aa9c9c1
Author | SHA1 | Message | Date |
---|---|---|---|
![]() |
6f3aa9c9c1 |
增加 WindowsService 类。
|
1 month ago |
![]() |
ef25ba94b9 |
Tray:增加 Instance 属性,作为唯一实例。
|
1 month ago |
![]() |
215e45cf88 |
Web:为允许解析 OPTIONS 方法提供选项,默认不解析,直接返回 0 字节 text/plain 内容。
|
1 month ago |
![]() |
69b7cf6fd9 |
增加扩展方法 Logger.UseFile(days)
|
1 month ago |
![]() |
4792113f05 |
Web:OPTIONS 方法直接返回,不再执行处理。
|
1 month ago |
![]() |
c5c71736a9 |
RegistryHelper:修正设置开机启动项的辅助方法。
|
1 month ago |
8 changed files with 140 additions and 44 deletions
-
26Apewer.Windows/Internals/RegistryHelper.cs
-
39Apewer.Windows/WinForm/Tray.cs
-
66Apewer.Windows/WinForm/WindowsService.cs
-
4Apewer.Windows/WindowsUtility.cs
-
4Apewer/Web/ApiOptions.cs
-
25Apewer/Web/ApiProcessor.cs
-
9Apewer/Web/ApiUtility.cs
-
11Apewer/_Extensions.cs
@ -0,0 +1,66 @@ |
|||||
|
using Apewer.Internals.Interop; |
||||
|
using System; |
||||
|
using System.Diagnostics; |
||||
|
using System.ServiceProcess; |
||||
|
|
||||
|
namespace Apewer.WinForm |
||||
|
{ |
||||
|
|
||||
|
/// <summary>Windows 服务。</summary>
|
||||
|
public class WindowsService : ServiceBase |
||||
|
{ |
||||
|
|
||||
|
Action on_start = null; |
||||
|
Action on_stop = null; |
||||
|
|
||||
|
/// <summary>创建 Windows 服务实例。</summary>
|
||||
|
/// <param name="serviceName">服务名称。</param>
|
||||
|
/// <param name="onStart">服务启动后,在后台线程执行的程序。</param>
|
||||
|
/// <param name="onStop">服务停止前,同步执行的程序。</param>
|
||||
|
/// <exception cref="ArgumentNullException"></exception>
|
||||
|
/// <exception cref="ArgumentException"></exception>
|
||||
|
public WindowsService(string serviceName, Action onStart, Action onStop) |
||||
|
{ |
||||
|
if (serviceName.IsEmpty()) throw new ArgumentNullException(nameof(serviceName)); |
||||
|
|
||||
|
ServiceName = serviceName; |
||||
|
on_start = onStart; |
||||
|
on_stop = onStop; |
||||
|
} |
||||
|
|
||||
|
/// <summary>创建 Windows 服务实例。</summary>
|
||||
|
/// <remarks>默认使用当前进程名称作为服务名称。</remarks>
|
||||
|
/// <param name="onStart">服务启动后,在后台线程执行的程序。</param>
|
||||
|
/// <param name="onStop">服务停止前,同步执行的程序。</param>
|
||||
|
/// <exception cref="ArgumentException"></exception>
|
||||
|
public WindowsService(Action onStart, Action onStop) : this(Process.GetCurrentProcess().ProcessName, onStart, onStop) { } |
||||
|
|
||||
|
/// <summary>服务启动时执行的程序。</summary>
|
||||
|
protected override void OnStart(string[] args) => RuntimeUtility.InBackground(on_start); |
||||
|
|
||||
|
/// <summary>服务停止时执行的程序。</summary>
|
||||
|
protected override void OnStop() => on_stop?.Invoke(); |
||||
|
|
||||
|
/// <summary>启动 Windows 服务实例。</summary>
|
||||
|
/// <param name="serviceName">服务名称。</param>
|
||||
|
/// <param name="onStart">服务启动后,在后台线程执行的程序。</param>
|
||||
|
/// <param name="onStop">服务停止前,同步执行的程序。</param>
|
||||
|
/// <exception cref="ArgumentNullException"></exception>
|
||||
|
/// <exception cref="ArgumentException"></exception>
|
||||
|
public static WindowsService Run(string serviceName, Action onStart, Action onStop = null) |
||||
|
{ |
||||
|
var service = new WindowsService(serviceName, onStart, onStop); |
||||
|
ServiceBase.Run(service); |
||||
|
return service; |
||||
|
} |
||||
|
|
||||
|
/// <summary>启动 Windows 服务实例。</summary>
|
||||
|
/// <remarks>默认使用当前进程名称作为服务名称。</remarks>
|
||||
|
/// <param name="onStart">服务启动后,在后台线程执行的程序。</param>
|
||||
|
/// <param name="onStop">服务停止前,同步执行的程序。</param>
|
||||
|
/// <exception cref="ArgumentException"></exception>
|
||||
|
public static WindowsService Run(Action onStart, Action onStop = null) => Run(Process.GetCurrentProcess().ProcessName, onStart, onStop); |
||||
|
|
||||
|
} |
||||
|
|
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue