惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

W
WeLiveSecurity
The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
Microsoft Azure Blog
Microsoft Azure Blog
The Register - Security
The Register - Security
Stack Overflow Blog
Stack Overflow Blog
博客园 - 三生石上(FineUI控件)
T
Threat Research - Cisco Blogs
S
SegmentFault 最新的问题
V2EX - 技术
V2EX - 技术
Hacker News: Ask HN
Hacker News: Ask HN
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
P
Proofpoint News Feed
J
Java Code Geeks
Microsoft Security Blog
Microsoft Security Blog
M
MIT News - Artificial intelligence
AI
AI
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
Hacker News - Newest:
Hacker News - Newest: "LLM"
B
Blog
N
News and Events Feed by Topic
N
News | PayPal Newsroom
Google DeepMind News
Google DeepMind News
酷 壳 – CoolShell
酷 壳 – CoolShell
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
WordPress大学
WordPress大学
C
Cybersecurity and Infrastructure Security Agency CISA
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 【当耐特】
U
Unit 42
腾讯CDC
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Cloudflare Blog
H
Help Net Security
Recent Announcements
Recent Announcements
P
Privacy & Cybersecurity Law Blog
IT之家
IT之家
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Security Archives - TechRepublic
Security Archives - TechRepublic
L
LINUX DO - 热门话题
Martin Fowler
Martin Fowler
MongoDB | Blog
MongoDB | Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
H
Heimdal Security Blog
博客园 - 聂微东
S
Securelist
大猫的无限游戏
大猫的无限游戏
Cloudbric
Cloudbric
Cisco Talos Blog
Cisco Talos Blog

博客园 - 暗香浮动

quartz.net 项目无法加载的问题 wcf的诡异问题 获取iTextSharp 的image 报错 FastReport 隐藏matrix的列如何实现 获取FileStream物理文件位置 log4net 1.2.11.0 的一点更新 无法更改数据库最大线程数 Moss文件操作速度慢的问题解决记录 无法更改数据库最大线程数 单用户模式恢复到多用户模式. mssql 数据库还原脚本 让客户ie8浏览器默认使用ie7兼容模式 - 暗香浮动 - 博客园 Package load Analyzer Create two fload when i run vs2008.here is solution to stop this. vss 命令行unpin批量操作 及vss的bug补丁 wf数据库Tracking服务 数据库表详解 生成随机字符串的问题 截断css影响的问题 clrprofile造成的日志肿瘤问题解决 webconfig中配置log4net 数据访问及业务层使用
windows 服务循环任务.服务启动后无法停止重启的解决办法
暗香浮动 · 2012-02-11 · via 博客园 - 暗香浮动

最近项目需要试用windows服务来监控一些数据的处理

protected override void OnStart(string[] args)
{

while(true)
{
try
{
workflow.ProcessWorkFlowMQ();
Thread.Sleep(150);
}
catch (Exception e1)
{
Logger.Error(e1.Message);
}
}
}

如上代码会造成 服务器安装成功后无法通过windows服务管理进行启动停止.比如说需要更换运行的服务器帐号就无法操作.

分析原因是因为OnStart方法无法执行结束.造成的.所以需要使用多线程来处理

OnStart方法内使用多线程 OnStop方法内部停止循环线程.测试通过服务可以正常启动停止.

代码如下

protected override void OnStart(string[] args)
{

Logger.Error("工作流监控信息启动!" + Environment.NewLine);
thread = new Thread(new ThreadStart(StartProcess));//启用另外一个线程来处理业务.否则 OnStart方法执行不完.服务无法进行停止启动操作.
thread.Start();

}

protected void StartProcess()
{
int i = 0;
while (true)
{

try
{
workflow.ProcessWorkFlowMQ();
Thread.Sleep(150);
i = 0;
}
catch (Exception e1)
{
//Logger.Error(e1.Message);
// i++;
// Thread.Sleep(500 * i);
// if (i == 200)
// {
// thread.Abort();
// }
}
}
}

protected override void OnStop()
{
thread.Abort();
}