在windows服务中需要帮助
本文关键字:帮助 windows 服务 | 更新日期: 2023-09-27 18:21:58
我写了一个windows服务,但当我安装它时,我发现它不起作用。我不知道是什么问题!
这个服务将一些文件从一个地方移动到另一个地方!
我需要的帮助
感谢
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
}
protected override void OnContinue()
{
const string SrcPath = "d:''Source";
const string DisPath = "d:''Dist''";
if (Directory.Exists(SrcPath))
{
string[] files = Directory.GetFiles(SrcPath);
for (int i = 0; i < files.Length; i++)
{
Console.WriteLine(files[i]);
string[] list = files[i].Split('''');
if (!File.Exists(DisPath + list[list.Length - 1]))
{
File.Move(files[i], DisPath + list[list.Length - 1]);
}
}
}
}
protected override void OnStop()
{
}
}
这是因为您在OnStart()
方法中没有执行任何操作。你需要有你想要的逻辑。当SCM发出"继续"命令时,OnContinue()
将被激发。
ServiceBase.OnStart()方法参考
为了补充Shark所说的内容,我想你不希望这只是做了一次工作,然后就死了。因此,你需要有某种机制来定期确定是否需要完成工作,然后完成工作,等待一段时间并重复。
否则,我建议您只考虑在windows任务调度程序上放置一个批处理脚本。
您必须在OnStart
中添加一些内容
System.Timers.Timer timer = new System.Timers.Timer();
protected override void OnStart(string[] args)
{
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
timer.Enabled = true;
timer.Interval = AppSettings.PeriodOfQuering;
}
void timer_Elapsed(object sender, ElapsedEventArgs e)
{
DoJob();
}