我在以下代码中找不到适合的方法来覆盖OnStart,OnStop,OnPowerEvent时遇到麻烦 [英] I have a trouble with finding a suitable method to override OnStart, OnStop, OnPowerEvent in the following code

查看:94
本文介绍了我在以下代码中找不到适合的方法来覆盖OnStart,OnStop,OnPowerEvent时遇到麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
 
public class MyMainServiceClass 
{
 
    private System.ComponentModel.Container components = null;
 
    private MyMainServiceClass m_mainApp; 
 

    public MyMainServiceClass ()
    {
 
        InitializeComponent ();
 

        CanHandlePowerEvent = true;
 

        m_mainApp = new MyMainServerAppClass ();
    } 
 
    static void Main ()
    {
        System.ServiceProcess.ServiceBase[] ServicesToRun;
 
        ServicesToRun = new System.ServiceProcess.ServiceBase[]
        {
            new MyMainServiceClass()
        };
 
        System.ServiceProcess.ServiceBase.Run (ServicesToRun);
    } 
 
    protected override void Dispose (bool disposing)
    {
        if (disposing)
        {
            if (components != null)
            {
                components.Dispose ();
            }
        }
        base.Dispose (disposing);
    } 
 
    protected override void OnStart (string[] args)
    {
 
        m_mainApp.Start ();
    }
 
    protected override void OnStop ()
    {
        m_mainApp.Stop ();
    }
 
    protected override bool OnPowerEvent (PowerBroadcastStatus powerStatus)
    {
        switch (powerStatus)
        {
        case PowerBroadcastStatus.QuerySuspend:
 
            return (true);
        case PowerBroadcastStatus.Suspend:
 
            m_mainApp.Stop ();
            break;
        case PowerBroadcastStatus.ResumeAutomatic:
        case PowerBroadcastStatus.ResumeCritical:
        case PowerBroadcastStatus.ResumeSuspend:
 
            m_mainApp.Start ();
            break;
        default:
            break;
        } 
        base.OnPowerEvent (powerStatus);
 
        return (true);
    } 
    private void InitializeComponent ()
    {
        components = new System.ComponentModel.Container ();
        this.ServiceName = "MyVS.NetService";
    }
 
}

推荐答案

术语覆盖"仅适用于派生自另一个类的类,以及适用于在类中声明的虚拟方法(无论是否抽象)基类.是您的基类System.ServiceProcess.ServiceBase http://msdn.microsoft.com/zh-我们/library/system.serviceprocess.servicebase.aspx [ ^ ]?

您的代码根本不显示继承.

如果您真的不了解这些内容,那么现在不应该进行Windows Service,也不要进行任何认真的开发.您首先需要在最基本的层次上提高对OOP的理解.对于简单的练习,最好使用最简单的控制台应用程序.学习继承,与虚拟",抽象"和覆盖"的后期绑定,局部性和范围,接口及其实现,静态和实例成员,然后回来.在执行任何UI之前,请学习代表和事件.等等.获取有关C#和.NET的基本手册,然后继续.如果错过了,任何开发都是无用的.不要浪费您和我们的时间,学习基础知识.

—SA
The term "override" only apply to a class derived from another class, and to a virtual method, abstract or not, declared in a base class. Is your base class System.ServiceProcess.ServiceBase, http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicebase.aspx[^]?

You code does not show inheritance at all.

If you really did not understand these things, you should not be doing Windows Service by now, as well as any serious development at all. You fist need to fix your understanding of OOP on a most elementary level. For simple exercises, better use simplest console application. Learn inheritance, late binding with ''virtual'', ''abstract'' and ''override'', locality and scope, interfaces and their implementation, static and instance member and come back. Learn delegates and events before doing any UI. And so on. Get an elementary manual on C# and .NET and proceed. If you miss it, any development is useless. Don''t waste your and our time, learn the basics.

—SA


这篇关于我在以下代码中找不到适合的方法来覆盖OnStart,OnStop,OnPowerEvent时遇到麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆