Calling ServiceBase.OnStart and OnStop… same instance?

[亡魂溺海] 提交于 2019-12-02 03:06:29

If you look in the Program.cs class, you'll see code like the following:

private static void Main()
{
    ServiceBase.Run(new ServiceBase[]
                {
                    new CometService()
                });
}

That is, the instance is created by code within your own project. It's that one instance that all Service Manager calls (including OnStart and OnStop) are made against.

I guess is the same instance. You can do a quick test adding a static field in the class to keep track of the reference to the object used in the OnStart and comparing it to the instance of the OnStop.

private static CometService instance = null;

protected override void OnStart(...)
{
    instance = this;
    ...
}

protected override void OnStop()
{
    object.ReferenceEquals(this, instance);
    ...
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!