C# 编写的windows service, 安装正常, 就是不能启动
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Da...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Timers;
using System.Runtime.InteropServices;
namespace Protecter
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
writeStartTime();
Timer runningTimer = new Timer();
runningTimer.Elapsed += new ElapsedEventHandler(writeRunningTime);
runningTimer.Interval = 5000;
runningTimer.Enabled = true;
GC.KeepAlive(runningTimer);
}
protected override void OnStop()
{
}
private void writeStartTime()
{
string logPath = @"C:\EthanCui\protecter.txt";
FileStream logStream = File.Open(logPath, FileMode.Append, FileAccess.ReadWrite);
StreamWriter logStreamWriter = new StreamWriter(logStream, Encoding.GetEncoding("gb2312"));
DateTime startTime = DateTime.UtcNow;
logStreamWriter.Write("Start at:" + startTime.ToString());
logStreamWriter.Close();
logStream.Close();
}
private static void writeRunningTime(object source, ElapsedEventArgs e)
{
string logPath = @"C:\EthanCui\protecter.txt";
FileStream logStream = File.Open(logPath, FileMode.Append, FileAccess.ReadWrite);
StreamWriter logStreamWriter = new StreamWriter(logStream, Encoding.GetEncoding("gb2312"));
DateTime startTime = DateTime.UtcNow;
logStreamWriter.Write("Running at:" + startTime.ToString());
logStreamWriter.Close();
logStream.Close();
}
}
}
我把OnStart() 里面的代码删掉,发现可以启动了,在网上搜,有人说启动方法里面的代码执行如果时间太长就会报错。那我应该怎么调整代码呢,我就是想每隔五秒钟,就将当前日期时间写入log文件中。 展开
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Timers;
using System.Runtime.InteropServices;
namespace Protecter
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
writeStartTime();
Timer runningTimer = new Timer();
runningTimer.Elapsed += new ElapsedEventHandler(writeRunningTime);
runningTimer.Interval = 5000;
runningTimer.Enabled = true;
GC.KeepAlive(runningTimer);
}
protected override void OnStop()
{
}
private void writeStartTime()
{
string logPath = @"C:\EthanCui\protecter.txt";
FileStream logStream = File.Open(logPath, FileMode.Append, FileAccess.ReadWrite);
StreamWriter logStreamWriter = new StreamWriter(logStream, Encoding.GetEncoding("gb2312"));
DateTime startTime = DateTime.UtcNow;
logStreamWriter.Write("Start at:" + startTime.ToString());
logStreamWriter.Close();
logStream.Close();
}
private static void writeRunningTime(object source, ElapsedEventArgs e)
{
string logPath = @"C:\EthanCui\protecter.txt";
FileStream logStream = File.Open(logPath, FileMode.Append, FileAccess.ReadWrite);
StreamWriter logStreamWriter = new StreamWriter(logStream, Encoding.GetEncoding("gb2312"));
DateTime startTime = DateTime.UtcNow;
logStreamWriter.Write("Running at:" + startTime.ToString());
logStreamWriter.Close();
logStream.Close();
}
}
}
我把OnStart() 里面的代码删掉,发现可以启动了,在网上搜,有人说启动方法里面的代码执行如果时间太长就会报错。那我应该怎么调整代码呢,我就是想每隔五秒钟,就将当前日期时间写入log文件中。 展开
2个回答
2014-01-23
展开全部
看看错误日志,如果没有 把错误日志输出来,看下具体什么错误才能调试。
追问
没有使用event log,小白第一次写服务,能安装,启动不了好像不能调试吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询