Wywołanie ServiceBase.OnStart i OnStop… ta sama instancja?
Mam więc usługę Windows napisaną w c #. Klasa usług pochodzi odServiceBase
oraz uruchamianie i zatrzymywanie usługi wywołuje metody instancjiOnStart
iOnStop
odpowiednio. Oto SSCE klasy:
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();
}
}
Jak widać, jest sporo logiki, która tego wymaga, gdy dzwonimyOnStop
, mamy do czynienia z tą samą instancjąServiceBase
jak wtedy, gdy zadzwoniliśmyOnStart
.
Czy mogę być pewien, że tak jest?