Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
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";
    }
 
}
Posted
Updated 22-Dec-11 3:41am
v2
Comments
Sandeep Mewara 22-Dec-11 9:48am    
Not clear.
JackDingler 22-Dec-11 12:00pm    
You have a number of technologies checked, that don't apply.
Sergey Alexandrovich Kryukov 22-Dec-11 12:02pm    
To start, fix your list of tags.
--SA

1 solution

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
 
Share this answer
 

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