15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

678 ❘ ChaPTer 25 windOws services<br />

The quoteServer variable is declared as a private member in the class:<br />

namespace Wrox.ProCSharp.WinServices<br />

{<br />

public partial class QuoteService: ServiceBase<br />

{<br />

private QuoteServer quoteServer;<br />

H<strong>and</strong>ler Methods<br />

When the service is stopped, the OnStop() method is called. You should stop the service functionality in<br />

this method:<br />

protected override void OnStop()<br />

{<br />

quoteServer.Stop();<br />

}<br />

In addition to OnStart() <strong>and</strong> OnStop(), you can override the following h<strong>and</strong>lers in the service class:<br />

➤<br />

➤<br />

➤<br />

➤<br />

➤<br />

OnPause() is called when the service should be paused.<br />

OnContinue() is called when the service should return to normal operation after being paused.<br />

To make it possible for the overridden methods OnPause() <strong>and</strong> OnContinue() to be called, the<br />

CanPauseAndContinue property must be set to true.<br />

OnShutdown() is called when Windows is undergoing system shutdown. Normally, the behavior<br />

of this method should be similar to the OnStop() implementation; if more time is needed for a<br />

shutdown, you can request additional time. Similarly to OnPause() <strong>and</strong> OnContinue(), a property<br />

must be set to enable this behavior: CanShutdown must be set to true.<br />

OnPowerEvent() is called when the power status of the system changes. The information<br />

about the change of the power status is in the argument of type PowerBroadcastStatus.<br />

PowerBroadcastStatus is an enumeration with values such as Battery Low <strong>and</strong><br />

PowerStatusChange. Here, you will also get information if the system would like to suspend<br />

(QuerySuspend), where you can approve or deny the suspend. You can read more about power events<br />

later in this chapter.<br />

OnCustomComm<strong>and</strong>() is a h<strong>and</strong>ler that can serve custom comm<strong>and</strong>s that are sent by a service<br />

control program. The method signature of OnCustomComm<strong>and</strong>() has an int argument where you<br />

get the custom comm<strong>and</strong> number. The value can be in the range from 128 to 256; values below<br />

128 are system-reserved values. In your service, you are re-reading the quotes file with the custom<br />

comm<strong>and</strong> 128:<br />

protected override void OnPause()<br />

{<br />

quoteServer.Suspend();<br />

}<br />

protected override void OnContinue()<br />

{<br />

quoteServer.Resume();<br />

}<br />

public const int comm<strong>and</strong>Refresh = 128;<br />

protected override void OnCustomComm<strong>and</strong>(int comm<strong>and</strong>)<br />

{<br />

switch (comm<strong>and</strong>)<br />

{<br />

case comm<strong>and</strong>Refresh:<br />

quoteServer.RefreshQuotes();<br />

break;<br />

default:<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!