Window service OnStop会立即关闭整个进程
来源:互联网 发布:java接收微信事件推送 编辑:程序博客网 时间:2024/06/06 23:46
注意:假如workerthread是一个死循环,我们的windows service能立即关掉吗?答案是是的。一旦主线程关闭,它不关心worker thread是否还在工作,立即关闭整个进程。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceProcess;
using System.Threading;
namespace WindowsServiceTest
{
classProgram : System.ServiceProcess.ServiceBase
{
///<summary>
/// name of the service
///</summary>
internalconststring Name = "Mediaroom test Service";
///<summary>
/// stop migration
///</summary>
privateManualResetEvent stopMigration = newManualResetEvent(false);
privateManualResetEvent stopMigration1 = newManualResetEvent(false);
///<summary>
/// thread for migration
///</summary>
privateThread migrationThread;
public Program()
{
this.ServiceName = Name;
}
///<summary>
/// Set things in motion so your service can do its work.
///</summary>
///<param name="args">arguments to start</param>
protectedoverridevoid OnStart(string[] args)
{
StartMigrationThread();
// Logger.LogInformation("{0} is started", this.ServiceName);
}
///<summary>
/// Stop this service.
///</summary>
protectedoverridevoid OnStop()
{
StopService();
}
protectedvoid StopService()
{
//stopMigration.Set();
//stopMigration1.WaitOne();
//Logger.LogInformation("{0} will be stopped soon after the processing jobs are completed.", this.ServiceName);
}
protectedvoid StartMigrationThread()
{
migrationThread =newThread(DoMigrationJobs);
migrationThread.IsBackground =true;
migrationThread.Start();
//Logger.LogInformation("Migration thread is started");
}
privatevoid DoMigrationJobs()
{
//while(!stopMigration.WaitOne(0))
while(true)
{
Thread.Sleep(1000);
}
// Thread.Sleep(3 * 60000);
//stopMigration1.Set();
}
staticvoid Main(string[] args)
{
System.ServiceProcess.ServiceBase[] servicesToRun;
// More than one user Service may run within the same process. To add
// another service to this process, change the following line to
// create a second service object. For example,
//
// servicesToRun = new System.ServiceProcess.ServiceBase[] {new SchedulerService(), new MySecondUserService()};
servicesToRun =new System.ServiceProcess.ServiceBase[] { newProgram() };
System.ServiceProcess.ServiceBase.Run(servicesToRun);
}
}
}
- Window service OnStop会立即关闭整个进程
- Android finish()并不会立即杀死进程
- C# 关闭窗体立即停止进程
- winform 关闭子窗体时关闭整个进程
- easyui-window 关闭事件,只要关闭窗口就会触发
- window、linux 下彻底关闭tomcat进程
- window环境变量立即生效
- Window根据进程名关闭进程的代码
- 立即退出当前进程
- Android如何立即退出整个程序
- android关闭整个应用程序
- Android 关闭整个应用程序
- Android 关闭整个应用程序
- 怎样关闭整个程序
- Android什么时候只会调用onPause()而不会调用onStop()?
- Android什么时候只会调用onPause()而不会调用onStop()?
- 关闭窗口,立即注销登陆
- js 定时器如何立即关闭
- VTK6 error:no override found for 'vtkRenderWindow'
- 多个线程调用静态方法,是否会出现并发问题?
- Numpy学习(4):自己动手制作类似于cifar10这样的图像数据集
- Material Design布局
- Magento 核心类 Varien_Data_Collections 应用介绍
- Window service OnStop会立即关闭整个进程
- 路由表简单介绍
- 从string类谈C++深浅拷贝的区别和意义
- PHP接收POST数据方式
- 【WHU1583】Palindrome-回文自动机+双向插入
- platform设备驱动
- shell环境,执行*.py 提示 No such file or directory
- Springboot+Jpa
- 浅谈CPU 指令 内存 硬盘间的关系