Click here to Skip to main content
16,017,922 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello Good morning EveryOne,

i have problem in window service it's Fire one error like

:member names cannot be the same as their enclosing type MyFirstWindowsService

my Code:
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Collections.Generic;
using System.ComponentModel;


namespace MyFirstWindowsService
{
    public class MyFirstWindowsService
    {

        static void Main()
        {
            ServiceBase[] ServicesToRun;
            Debugger.Break();
            ServicesToRun = new ServiceBase[] 
		{ 
			new MyFirstWindowsService() 
		};
            ServiceBase.Run(ServicesToRun);
        }


        public partial class MyFirstWindowsService : ServiceBase
        {

            public MyFirstWindowsService()
            {
                InitializeComponent();
            }

            protected override void OnStart(string[] args)
            {
                timer.Enabled = true;
                timer.Interval = 10000;
                timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
            }

            protected override void OnStop()
            {
            }
            private System.ComponentModel.IContainer components = null;

            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }



            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            System.Timers.Timer timer = new System.Timers.Timer();
            public void MyFirstWindowsService()//Problem is Here
            {
                InitializeComponent();
                this.CanStop = true;
                this.CanPauseAndContinue = true;

            }
            private void InitializeComponent()
            {
                components = new System.ComponentModel.Container();
                this.ServiceName = "Service1";
            }

            protected void timer_Elapsed(object source, System.Timers.ElapsedEventArgs aa)
            {
                System.Console.WriteLine("myTimer event occurred");
                //Here we can write beautiful code that performs some our own tasks.
            }
        }

    }
}
Posted

Looks like you gave several methods the same name, you cant do that, the program would get totally confused by it.
 
Share this answer
 
Hi,
Change your partial class name and its constructor name, because the nae of the main class and partial class cannot be same. Try this:
C#
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Collections.Generic;
using System.ComponentModel;


namespace MyFirstWindowsService
{
    public class MyFirstWindowsService
    {

        static void Main()
        {
            ServiceBase[] ServicesToRun;
            Debugger.Break();
            ServicesToRun = new ServiceBase[] 
		{ 
			new Test() 
		};
            ServiceBase.Run(ServicesToRun);
        }


        public partial class Test : ServiceBase
        {

            public Test()
            {
                InitializeComponent();
            }

            protected override void OnStart(string[] args)
            {
                timer.Enabled = true;
                timer.Interval = 10000;
                timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
            }

            protected override void OnStop()
            {
            }
            private System.ComponentModel.IContainer components = null;

            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }



            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            System.Timers.Timer timer = new System.Timers.Timer();
            public void MyFirstWindowsService()//Problem is Here
            {
                InitializeComponent();
                this.CanStop = true;
                this.CanPauseAndContinue = true;

            }
            private void InitializeComponent()
            {
                components = new System.ComponentModel.Container();
                this.ServiceName = "Service1";
            }

            protected void timer_Elapsed(object source, System.Timers.ElapsedEventArgs aa)
            {
                System.Console.WriteLine("myTimer event occurred");
                //Here we can write beautiful code that performs some our own tasks.
            }
        }

    }
}



All the best.
-=-Amit
 
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