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();
} }
}
就如以上那么简单,却启动不了,总是"服务没有及时响应或控制请求"
我现在改成了这样还是不行呢~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();
} }
}
成功的话,用VS的debug注入window service进程,可以调试。
怎么用线程来启动OnStart()a ?
用InstallUtil.exe 来部署的啊,~ 部署成服务时没问题的`` 就是不知道那点代码是不是写错了```
生成serviceProcessInstaller1 serviceInstaller1了没?
测试时Account我是设为LocalSystem了
serviceInstaller1.Parent设为serviceProcessInstaller1