Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Ok, so I'm writing a simple Windows Service that keeps track of a few Registry entries periodically. I want to send out an alert email when the service is stopped by a human.

I found this interesting quote when I was looking around:
You can check the close condition to see if it is Task Manager and if it is, then set the 'cancel' value to true - meaning cancel the closing.

I don't know what this guy means. What's the "close condition"?
Can anyone elaborate on how to do that kind of stuff?


Thanks!
Posted
Comments
Sergey Alexandrovich Kryukov 20-Jan-13 15:08pm    
Why should we consider any gibberish written who know by who? Who cares what it means?
This is not a reasonable question at all.
—SA
Casey Sheridan 20-Jan-13 15:15pm    
The key is not the quote. The key is how to detect what is closing it.

I'm expecting people who know how to do this to respond with their knowledge.
Sergey Alexandrovich Kryukov 20-Jan-13 15:19pm    
Are you trying to be rude? It sounds like. I provided you a comprehensive answer, did you notice that?
—SA
Casey Sheridan 20-Jan-13 15:24pm    
The answer wasn't up yet.
And, yes, I was a little frustrated by your first comment.
I'm sorry.
Sergey Alexandrovich Kryukov 20-Jan-13 15:56pm    
No problem. I'm sorry if you have been frustrated. But for a record: if someone does not give you the answer, it's not a reason to suggest this person cannot. I personally try to use positive presumption.

I can only explain that it is just a criticism of your approach you tried to use in this very point, without any personal implications, whatsoever. If wrong approaches were not criticized, how the good ones would come? The criticism is needed to help and should better be used constructively: either argument for your point of improve yourself. I'm actually sure you understand it well yourself... Hope we are quite good. :-)
—SA

1 solution

The request is so incorrect! Please see my comment to the question. If you think that any foolishness posted on the Web makes sens, it can become a huge problem for you.

Also, the whole idea of having the service to poll any condition on periodic basic is wrong. So, first thing I would say is: you don't need such "service". You waste CPU time on checking up something when nothing happens; at the same time, you can easily miss the event, because someone can change your registry between your polls. You are doing something which hardly can make any sense. Perhaps if you explain why doing so, you can get a chance for some reasonable advice.

[EDIT; in connection to the follow-up question:]

The problem is not a search language, the problem is polling, which is always bad, even in simpler cases; I explained a part of it above.

For your understanding of the polling (pull vs push), please see:
http://en.wikipedia.org/wiki/Push_technology[^],
http://en.wikipedia.org/wiki/Pull_technology[^].

In particular, this class provides a benefit of the push approach:
http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx[^].

You subscribe to a certain class of events on the file system, and all modifications are pushed to your handler code.

I don't say this class could be a best solution for you, it's more to give you the idea. I may or may not know the ultimate solution about the Registry issue; it depends on what you want to achieve, exactly. Why doing so?

Anyway, is you really need to detect registry changes, you need to use the push technology. One way to achieve this is subscribing to the events using WMI. Please see:
http://msdn.microsoft.com/en-us/library/aa393035.aspx[^].

[END EDIT]

As to the stopping a service by human, the solution is trivial. In your implementation of the class System.ServiceProcess.ServiceBase, you can handle the Stop even by overriding the virtual OnStop method:
http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicebase.aspx[^],
http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicebase.onstop.aspx[^].

As simple as that. Problem solved.

—SA
 
Share this answer
 
v3
Comments
Casey Sheridan 20-Jan-13 15:23pm    
Ok. Thanks for the solution.
Although, please suggest if there is a better way to learn than to search online. I was just doing what most people ask for.
Sergey Alexandrovich Kryukov 20-Jan-13 15:42pm    
You are very welcome.

I basically answered to this question, too. Please see my updated answer, after [EDIT].
If you cannot finally solve your problem, which is unrelated to your initial concern about Stop, it will need some discussion. You should better create a separate question. If you do so and want me to participate, please leave me some notification by replying to this post.

Good luck,
—SA
Casey Sheridan 20-Jan-13 16:31pm    
Thanks for the extra info.

I believe that I'll try to experiment for a little while before posting a new question. However, if I do post again, you are more than welcome to help.

Thank you also for the critique. I think that will help me.
Sergey Alexandrovich Kryukov 20-Jan-13 16:47pm    
Good idea. Remember, you are welcome to ask in case of other concerns.
Good luck,
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900