开发者

IDisposable code on process termination

开发者 https://www.devze.com 2023-04-11 19:17 出处:网络
I have a good old fashioned windows service (inheriting from System.ServiceProcess.ServiceBase), to which I added a component (implementing IComponent) by this.components.Add(new MyStuff());

I have a good old fashioned windows service (inheriting from System.ServiceProcess.ServiceBase), to which I added a component (implementing IComponent) by this.components.Add(new MyStuff());

However MyStuff's Disposable() doesn't run if I shut down the exe from Task Manager.

Possible suspects:

  • Nothing runs on "End Process". If so, how do I clean up after myself (e.g. kill started processes?)
  • For testing purposes I start my service with

    var service = new MyService();

    service.Run();

    Thread.Wait(Timeout.Infinite);

    instead of ServiceBase.Run(new []{new MyService()}); can that be the problem?

  • ServiceBase doesn't clean up automaticly. If so, w开发者_StackOverflowhat should I do?

Please help!

Additional info: I'm trying to shut down other exes I've started from my service using Process.Start(...), so if there's a way to make them auto-shutdown (child process?) that would be fine too.


My typical way of having children detect the death of a parent:

  • During parent startup, generate a nice, long, random name
  • Also during startup, obtain a named mutex with this name, and keep that held locked for the lifetime of the parent.
  • When starting a child process, pass the long random name as a parameter
  • In the children, dedicate a thread that attempts to obtain the same named mutex.
  • If the child ever obtains the mutex, the parent process must have died - so the child should exit also

By using a random name for the mutex, you ensure that a new hierarchy can be constructed even whilst the older parent/child hierarchy is in the process of being shut down.


If you "pull the plug" there is nothing you can do at that moment. It is not considered a controlled shutdown. It is totally unexpected and nothing will give you the power to handle that from the inside of the process that is killed. It is not even considered a shutdown. To make that clear: You are not shutting down, you are killing it off! Its like ripping the power cord out of your computer and expecting it to shutdown gracefully. Wont work (though i had a tester write this very exact testcase for me a few years ago...)

Solve it:

  • Your child processes would need to "ping" the parent process. If it is not responding, you know that something is wrong and shutdown gracefully.

  • Or refer to the answer of ChrisBint (Service Controller). Get the service by name, start and stop it.

Find an example code here: http://www.csharp-examples.net/restart-windows-service/


Why are you stopping the service using Task Manager, you are basically killing it, not asking it to do something.

Put some code in the OnStop method to clean up and use Service Manager to stop/start the service.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号