using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using System.IO;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using Config;
namespace Statustics
{
    partial class StatisticsService : ServiceBase
    {
        public StatisticsService()
        {
            InitializeComponent();
        }        protected override void OnStart(string[] args)
        {
            this.timer1.Enabled = true;
        }
        protected override void OnStop()
        {
            // TODO: 在此处添加代码以执行停止服务所需的关闭操作。
            this.timer1.Enabled = false;
        }        private void timer1_Tick(object sender, EventArgs e)
        {            StreamWriter write = new StreamWriter(@"c:\asd.txt");
            write.Write("asdasd");
            write.Close();
        }    }
}
就如以上那么简单,却启动不了,总是"服务没有及时响应或控制请求"

解决方案 »

  1.   

    还不知道windows服务是啥那?不过见过系统的自带的服务,终于知道了自己空缺的一大块!
      

  2.   

    this.timer1 不会是 System.Windows.Forms.Timer 吧?
      

  3.   

    windows服务中如果涉及UI的话。。安装服务后,要在该服务-属性-登录登录身份选择 本地系统账号,并勾选允许与桌面交互
      

  4.   


    我现在改成了这样还是不行呢~using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Diagnostics;
    using System.Linq;
    using System.IO;
    using System.ServiceProcess;
    using System.Text;
    using System.Threading;
    using Config;
    namespace Statustics
    {
        partial class StatisticsService : ServiceBase
        {
            private System.Timers.Timer timer = new System.Timers.Timer();
            public StatisticsService()
            {
                InitializeComponent();
             
            }        protected override void OnStart(string[] args)
            {
                this.timer.Interval = 1000;
                this.timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
                this.timer.Enabled = true;
            }
            protected override void OnStop()
            {
                // TODO: 在此处添加代码以执行停止服务所需的关闭操作。
                this.timer.Enabled = false;
            }
            void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
            {
                StreamWriter write = new StreamWriter(@"c:\asd.txt");
                write.Write("asdasd");
                write.Close();
            }    }
    }
      

  5.   

    想在debug状态调试Server吧,得自己加个线程,由线程来启动Onstart就行
      

  6.   

    this.timer1.Enabled  你是用的时钟控件吗?服务程序不支持时钟控件,需要用Timer 类来做
      

  7.   

    安装成功了吗?
    成功的话,用VS的debug注入window service进程,可以调试。
      

  8.   


    怎么用线程来启动OnStart()a ?
      

  9.   

    还有window service程序多写点log,否则不在本机的时候,就麻烦了。
      

  10.   


    用InstallUtil.exe 来部署的啊,~ 部署成服务时没问题的`` 就是不知道那点代码是不是写错了```
      

  11.   

    怎么用线程来启动OnStart()a ?
      

  12.   

    测试过正常(System.Timers.Timer)。可能是部署没做好了。
    生成serviceProcessInstaller1  serviceInstaller1了没?
    测试时Account我是设为LocalSystem了
    serviceInstaller1.Parent设为serviceProcessInstaller1
      

  13.   

    http://support.microsoft.com/kb/816169/zh-cn