该服务未在C#中及时响应启动或控制请求

时间:2015-10-06 11:31:36

标签: c# service windows-services

我使用c#在.net framework 4中创建了windows服务。我成功地将它安装到服务中。但当我尝试启动它时,它表示“服务没有及时响应启动或控制请求”

这是我的代码

public partial class Service1 : ServiceBase
    {
        private Service_Bll objService_Bll = new Service_Bll();

        public Service1()
        {
            InitializeComponent();

        }
        System.Timers.Timer timer;
        protected override void OnStart(string[] args)
        {
            this.WriteToFile("Simple Service started {0}");
            Thread t = new Thread(new ThreadStart(this.InitTimer));
            t.Start();
        }

        private void InitTimer()
        {
            timer = new System.Timers.Timer();
            //wire up the timer event 
            timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
            //set timer interval   
            //var timeInSeconds = Convert.ToInt32(ConfigurationManager.AppSettings["TimerIntervalInSeconds"]); 
            timer.Interval = (10 * 1000);
            // timer.Interval is in milliseconds, so times above by 1000 
            timer.Enabled = true;
        }


        protected override void OnStop()
        {
            this.WriteToFile("Simple Service stopped {0}");
        }


        private void WriteToFile(string text)
        {
            string path = "C:\\ServiceLog.txt";
            using (StreamWriter writer = new StreamWriter(path, true))
            {
                writer.WriteLine(string.Format(text, DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt")));
                writer.Close();
            }
        }




        public void ScheduleService()
        {
            try
            {
                objService_Bll.UpdateAccessDetails();
            }
            catch (Exception ex)
            {
                WriteToFile("Simple Service Error on: {0} " + ex.Message + ex.StackTrace);

                //Stop the Windows Service.
                using (System.ServiceProcess.ServiceController serviceController = new System.ServiceProcess.ServiceController("Service1"))
                {
                    serviceController.Stop();
                }
            }
        }

        protected void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            this.WriteToFile("Simple Service run {0}");
            ScheduleService();
        }

    }

这是intilizecomponent()

private void InitializeComponent()
    {
        this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
        this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
        // 
        // serviceProcessInstaller1
        // 
        this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
        this.serviceProcessInstaller1.Password = null;
        this.serviceProcessInstaller1.Username = null;
        // 
        // serviceInstaller1
        // 
        this.serviceInstaller1.ServiceName = "Service1";
        this.serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
        // 
        // ProjectInstaller
        // 
        this.Installers.AddRange(new System.Configuration.Install.Installer[] {
        this.serviceProcessInstaller1,
        this.serviceInstaller1});

    }

我无法想象这一点。请帮忙。非常感谢你。

0 个答案:

没有答案