
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
1088
|
Chapter 20: Toolbox
The notifications from SystemEvents come on a dedicated thread for raising these
events. In a Windows Forms application, you will need to get back on to the correct
user interface thread before updating a UI with any of this information, using one of
the various methods for doing so (
Control.BeginInvoke, Control.Invoke, BackgroundWorker).
See Also
See the “SystemEvents Class,” “PowerModeChangedEventArgs Class,” “Session-
EndedEventArgs Class,” “SessionEndingEventArgs Class,” “SessionSwitchEvent-
Args Class,” “TimerElapsedEventArgs Class,” “UserPreferenceChangingEventArgs
Class,” and “UserPreferenceChangedEventArgs Class” topics in the MSDN
documentation.
20.2 Controlling a Service
Problem
You need to programmatically manipulate a service that your application interacts
with.
Solution
Use the System.ServiceProcess.ServiceController class to control the service.
ServiceController allows you to interact with an existing service and to read and
change its properties. In the example, it will be used to manipulate the ASP.NET
State Service. The name, the service type, and the display name are easily available
from the
ServiceName, ServiceType, and DisplayName properties.
ServiceController scStateService = new ServiceController("COM+ Event System");
Console.WriteLine("Service ...