简体   繁体   English

WindowsService中OnStart()内部的代码段是否重复?

[英]Does segment of code inside the OnStart() in WindowsService repeat?

I learnt that the WindowsService`s OnStart() executes the segment of code inside it? 我了解到WindowsService的OnStart()执行其中的代码段吗? Does the segment of code execute only once, which is at the start of process or does it get executed repeatedly until the process stops? 代码段是仅在过程开始时执行一次还是在过程停止之前重复执行?

It runs once, and you have to return from it before your service is considered to be started - so if you want something to run continuously, it's up to you to create eg new threads or timers to run that code. 它只运行一次,您必须先从它return ,然后才能认为您的服务被启动-因此,如果您希望某项东西连续运行,则要由自己创建例如新线程或计时器来运行该代码。

You would also, usually, at this time also set up any needed synchronization primitives (such as ManualResetEvent , or a CancellationTokenSource ) so that you can use these inside your OnStop method to get your new threads/other code to exit gracefully. 通常,这时您通常还需要设置任何所需的同步原语(例如ManualResetEventCancellationTokenSource ),以便可以在OnStop方法中使用这些原语来使新线程/其他代码正常退出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM