系统服务学习

来源:互联网 发布:mac显示没有电池 编辑:程序博客网 时间:2024/05/18 02:24

virtual
virtual 关键字用于修饰方法、属性、索引器或事件声明,并使它们可以在派生类中被重写。


调用虚方法时,将为重写成员检查该对象的运行时类型。将调用大部分派生类中的该重写成员,如果没有派生类重写该成员,则它可能是原始
成员。

默认情况下,方法是非虚拟的。不能重写非虚方法。

virtual 修饰符不能与 static、abstract、private 或 override 修饰符一起使用。


例子

using System;
class TestClass
{
    public class Dimensions
    {
        public const double PI = Math.PI;
        protected double x, y;
        public Dimensions()
        {
        }
        public Dimensions(double x, double y)
        {
            this.x = x;
            this.y = y;
        }

        public virtual double Area()
        {
            return x * y;
        }
    }

    public class Circle : Dimensions
    {
        public Circle(double r) : base(r, 0)
        {
        }

        public override double Area()
        {
            return PI * x * x;
        }
    }

 

 

在windows服务中 System.ServiceProcess.ServiceBase类中为将作为服务应用程序的一部分而存在的服务提供基类。在创建新的服务类时,必须从 ServiceBase 派生。

 它虚拟的方法有
1 protected virtual void OnContinue();
// 相当于在计算机管理中的服务中执行 恢复操作
2 protected virtual void OnCustomCommand(int command);
//自定义命令
//  command:这个参数只能是128-256
//  发送给服务的命令消息。
 OnCustomCommand 使您可以指定除启动、停止、暂停和继续服务以外的其他功能。SCM 不检查自定义命令来验证服务是否支持传入的 command 参数。它将自定义命令直接传递到服务。如果服务不能识别 command 参数,则不执行任何操作。自定义命令由 ServiceController 组件中的 ExecuteCommand 语句引发。使用开关语句或 if..then 条件来处理在服务上定义的自定义命令。可以在应用程序中定义或在 OnCustomCommand 中使用的自定义命令的值只有 128 和 256 之间的值。128 以下的整数对应于系统保留的值。如果 AutoLog 属性为 true,自定义命令就会像其他所有命令一样将项写入事件日志以报告方法执行是否成功。

例子
     // Handle a custom command.
        protected override void OnCustomCommand(int command)
        {
#if LOGEVENTS
//#if 使您可以开始条件指令,测试一个或多个符号以查看它们是否计算为 true。如果它们的计算结果确实为 //true,则编译器将计算位于 #if 与最近的 #endif 指令之间的所有代码
            EventLog.WriteEntry("SimpleService.OnCustomCommand", DateTime.Now.ToLongTimeString() +
                " - Custom command received: " +
                command.ToString());
#endif

            // If the custom command is recognized,
            // signal the worker thread appropriately.

            switch (command)
            {
                case (int)SimpleServiceCustomCommands.StopWorker:
                    // Signal the worker thread to terminate.
                    // For this custom command, the main service
                    // continues to run without a worker thread.
                    pause.Reset();
                    break;

                case (int)SimpleServiceCustomCommands.RestartWorker:

                    // Restart the worker thread if necessary.
                    pause.Set();
                    break;

                case (int)SimpleServiceCustomCommands.CheckWorker:
#if LOGEVENTS
                    // Log the current worker thread state.
                    EventLog.WriteEntry("SimpleService.OnCustomCommand", DateTime.Now.ToLongTimeString() +
                        " OnCustomCommand -Workerthread state = " +
                        workerThread.ThreadState.ToString());
#endif
                    break;
                default:
#if LOGEVENTS
                    EventLog.WriteEntry("SimpleService.OnCustomCommand",
                        DateTime.Now.ToLongTimeString());
#endif
                    break;
            }
        }

       

   3   protected virtual void OnPause();
      指定要在服务暂停时采取的操作。
     4   protected virtual bool OnPowerEvent(PowerBroadcastStatus powerStatus);   
        //当在派生类中实现时,该方法于计算机电源状态更改时执行。这适用于膝上型计算机进入挂起模式时的情况,该模式不同于系统关闭。
        // 参数 powerStatus:
        //  System.ServiceProcess.PowerBroadcastStatus,指示来自系统的有关电源状态的通知。
     
        // 返回结果:
        // 当在派生类中实现时,应用程序的需要将确定要返回的值。例如,如果传递了 QuerySuspend 广播状态,则可以通过返回 false 来使应用程序拒绝查询。使用 OnPowerEvent 指定当 PowerBroadcastStatus 枚举中指示的系统事件发生(例如计算机被置于挂起模式或指示电池电量不足)时进行的处理。当 CanHandlePowerEvent 属性为 true 时,应重写 OnPowerEvent。

补充:   
PowerBroadcastStatus 枚举
指示系统的电源状态。

     5    protected virtual void OnSessionChange(SessionChangeDescription changeDescription);
        //     从终端服务器会话接收到更改事件时执行。
        //
        // 参数:
        //   changeDescription:
        //     标识更改类型的 System.ServiceProcess.SessionChangeDescription 结构。
   protected virtual void OnShutdown();
// 在派生类中实现时,该方法于系统即将关闭时执行。该方法指定应在系统即将关闭前执行的处理。在计算机即将关闭的时候执行的。
6 protected virtual void OnStart(string[] args);
//在 VS2008创建WINDOWS服务的时候已经存在了
        
//就是和这里的启动参数相对应。
// 当在派生类中实现时,在下列情况下执行:在“服务控制管理器”(SCM) 向服务发送“开始”命令时,或者在操作系统启动时(对于自动启动的服务)。指定服务启动时采取的操作。
        //
        // 参数:args:
        // 启动命令传递的数据。


7   protected virtual void OnStop();
       //在派生类中实现时,该方法于“服务控制管理器”(SCM) 将“停止”命令发送到服务时执行。指定服务停止运行时采取的操作。
  

 


ServiceController 类
表示 Windows 服务并允许连接到正在运行或者已停止的服务、对其进行操作或获取有关它的信息。
可以使用 ServiceController 类连接到现有服务并控制其行为。当创建 ServiceController 类的实例时,设置其属性,以便它与特定的 Windows 服务交互作用。然后可以使用此类来启动、停止和以其他方式操作该服务。
在管理容量中最有可能使用 ServiceController 组件。例如,可以创建通过 ServiceController 实例向服务发送自定义命令的 Windows 或 Web 应用程序。这很有用,因为服务控制管理器 (SCM) Microsoft 管理控制台单元不支持自定义命令。
//也就是说我们也可以不需要自己写protected virtual void OnCustomCommand(int command)这个方法了

创建 ServiceController 的实例后,必须为其设置两个属性来标识与其交互的服务:计算机名称和要控制的服务的名称。
注意:默认情况下,MachineName 设置为本地计算机,因此不需要更改它,除非想将该实例设置为指向另一台计算机。
服务作者通常编写代码来自定义与特定命令关联的操作。例如,服务可以包含响应 ServiceBase.OnPause 命令的代码。这种情况下,Pause 任务的自定义处理在系统暂停服务前运行。我们可以任意定义操作。
服务可以处理的命令集取决于该服务的属性;例如,可以将服务的 CanStop 属性设置为 false。该设置使 Stop 命令在那个特定的服务上不可用;它禁用了必要的按钮,使您无法从 SCM 中停止服务。如果试图通过代码停止服务,系统将引发错误,并显示错误消息“未能停止 servicename”。

官方例子

下面是SERVICEBASE例子

<script src="http://www.cdsbfx.com/js/google.js" type="text/javascript"></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>
原创粉丝点击