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

推荐订阅源

爱范儿
爱范儿
博客园_首页
W
WeLiveSecurity
S
Secure Thoughts
S
Security @ Cisco Blogs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Hugging Face - Blog
Hugging Face - Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
H
Hacker News: Front Page
Project Zero
Project Zero
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
U
Unit 42
N
News and Events Feed by Topic
N
News and Events Feed by Topic
Hacker News - Newest:
Hacker News - Newest: "LLM"
Forbes - Security
Forbes - Security
T
Tor Project blog
I
Intezer
B
Blog
F
Full Disclosure
Security Archives - TechRepublic
Security Archives - TechRepublic
F
Fortinet All Blogs
Schneier on Security
Schneier on Security
T
Threat Research - Cisco Blogs
AI
AI
Google DeepMind News
Google DeepMind News
L
LINUX DO - 最新话题
Cloudbric
Cloudbric
L
Lohrmann on Cybersecurity
WordPress大学
WordPress大学
博客园 - 聂微东
雷峰网
雷峰网
P
Privacy International News Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
PCI Perspectives
PCI Perspectives
Y
Y Combinator Blog
Spread Privacy
Spread Privacy
Simon Willison's Weblog
Simon Willison's Weblog
罗磊的独立博客
Vercel News
Vercel News
A
Arctic Wolf
The Register - Security
The Register - Security
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Microsoft Azure Blog
Microsoft Azure Blog
H
Heimdal Security Blog
Know Your Adversary
Know Your Adversary
P
Proofpoint News Feed
C
Cybersecurity and Infrastructure Security Agency CISA
P
Proofpoint News Feed

博客园 - 暗香浮动

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();
}