Window service OnStop会立即关闭整个进程



注意:假如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

{

class Program : System.ServiceProcess.ServiceBase

{

/// <summary>

/// name of the service

/// </summary>

internal const string Name = "Mediaroom test Service";

/// <summary>

/// stop migration

/// </summary>

private ManualResetEvent stopMigration = new ManualResetEvent(false);

private ManualResetEvent stopMigration1 = new ManualResetEvent(false);

/// <summary>

/// thread for migration

/// </summary>

private Thread 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>

protected override void OnStart(string[] args)

{

StartMigrationThread();

// Logger.LogInformation("{0} is started", this.ServiceName);

}

/// <summary>

/// Stop this service.

/// </summary>

protected override void OnStop()

{

StopService();

}

protected void StopService()

{

//stopMigration.Set();

//stopMigration1.WaitOne();

//Logger.LogInformation("{0} will be stopped soon after the processing jobs are completed.", this.ServiceName);

}

protected void StartMigrationThread()

{

migrationThread = new Thread(DoMigrationJobs);

migrationThread.IsBackground = true;

migrationThread.Start();

//Logger.LogInformation("Migration thread is started");

}

private void DoMigrationJobs()

{

//while(!stopMigration.WaitOne(0))

while(true)

{

Thread.Sleep(1000);

}

           // Thread.Sleep(3 * 60000);

//stopMigration1.Set();

}

static void 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[] { new Program() };

System.ServiceProcess.ServiceBase.Run(servicesToRun);

}

}

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值