Llamando a ServiceBase.OnStart y OnStop… ¿la misma instancia?
Así que tengo un servicio de Windows escrito en c #. La clase de servicio deriva deServiceBase
, e iniciando y parando los métodos de instancia de llamadas de servicio.OnStart
yOnStop
respectivamente. Aquí está SSCE de la clase:
partial class CometService : ServiceBase
{
private Server<Bla> server;
private ManualResetEvent mre;
public CometService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
//starting the server takes a while, but we need to complete quickly
//here so let's spin off a thread so we can return pronto.
new Thread(() =>
{
try
{
server = new Server<Bla>();
}
finally
{
mre.Set()
}
})
{
IsBackground = false
}.Start();
}
protected override void OnStop()
{
//ensure start logic is completed before continuing
mre.WaitOne();
server.Stop();
}
}
Como puede verse, hay mucha lógica que requiere que cuando llamamosOnStop
, estamos tratando con la misma instancia deServiceBase
como cuando llamamosOnStart
.
¿Puedo estar seguro de que este es el caso?