schangxiang@126.com
2025-09-09 3d8966ba2c81e7e0365c8b123e861d18ee4f94f5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System.ServiceProcess;
using Common.Logging;
using System.Threading;
using WIP_Print;
using System;
using System.Threading.Tasks;
 
namespace WIP_PrintService
{
    public partial class WIP_PrintService : ServiceBase
    {
        private readonly ILog logger;
        static CancellationTokenSource cancelTokenSource = new CancellationTokenSource();
        public WIP_PrintService()
        {
            InitializeComponent();
            logger = LogManager.GetLogger(GetType());
            Task.Factory.StartNew(PrintThread, cancelTokenSource.Token);
        }
 
        protected override void OnStart(string[] args)
        {
            logger.Info("打印服务成功启动");
        }
 
        protected override void OnStop()
        {
            cancelTokenSource.Cancel();
            logger.Info("打印服务成功终止");
        }
 
        protected override void OnPause()
        {
        }
 
        protected override void OnContinue()
        {
        }
 
 
        private void PrintThread()
        {
            //判断是否取消任务
            while (!cancelTokenSource.IsCancellationRequested)
            {
                try
                {
                    logger.Info("一次打印开始");
                    PrintService.GetInstance().Print();
                    logger.Info("一次打印结束");
                }
                catch (Exception e)
                {
                    logger.Error("一次打印错误:" + e.Message, e);
                }
                Thread.Sleep(5000);//休眠5秒
            }
        }
    }
}