C# 如何使windows服务在重复此过程之前完成所有代码
因此,我创建了一个服务,在服务器上运行,查看不同的文件夹共享,并将它们放在数据库表中进行自定义搜索,但它会将搜索位置的数据表拉入其中,并一次遍历一个,这对于小型测试文件夹非常有效。现在,我试图真正使用它,但在计时器重新启动之前,它无法通过第一个文件夹路径。我可以延长它的时间,但我基本上希望它持续运行,并在第一次运行完成后立即重新开始,或者不确定是否可以同时运行所有路径。我让它每30分钟运行一次,但肯定不够长C# 如何使windows服务在重复此过程之前完成所有代码,c#,.net,service,C#,.net,Service,因此,我创建了一个服务,在服务器上运行,查看不同的文件夹共享,并将它们放在数据库表中进行自定义搜索,但它会将搜索位置的数据表拉入其中,并一次遍历一个,这对于小型测试文件夹非常有效。现在,我试图真正使用它,但在计时器重新启动之前,它无法通过第一个文件夹路径。我可以延长它的时间,但我基本上希望它持续运行,并在第一次运行完成后立即重新开始,或者不确定是否可以同时运行所有路径。我让它每30分钟运行一次,但肯定不够长 using System; using System.Collections.Gener
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.Threading.Tasks;
using System.Timers;
namespace MFTSearchService
{
public partial class MFTSearchService : ServiceBase
{
Timer timer = new Timer(); // name space(using System.Timers;)
public MFTSearchService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
WriteToFile("Service is started at " + DateTime.Now);
timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
timer.Interval = TimeSpan.FromMinutes(10).TotalMilliseconds; //number in milisecinds
timer.Enabled = true;
//global::MFTSearchService.Search.SearchStart();
}
protected override void OnStop()
{
WriteToFile("Service is stopped at " + DateTime.Now);
}
private void OnElapsedTime(object source, ElapsedEventArgs e)
{
WriteToFile("Service is recall at " + DateTime.Now);
timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
timer.Interval = TimeSpan.FromMinutes(30).TotalMilliseconds; //number in milisecinds
timer.Enabled = true;
global::MFTSearchService.Search.SearchStart();
}
public void WriteToFile(string Message)
{
string path = AppDomain.CurrentDomain.BaseDirectory + "\\Logs";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
string filepath = AppDomain.CurrentDomain.BaseDirectory + "\\Logs\\ServiceLog_" + DateTime.Now.Date.ToShortDateString().Replace('/', '_') + ".txt";
if (!File.Exists(filepath))
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(filepath))
{
sw.WriteLine(Message);
}
}
else
{
using (StreamWriter sw = File.AppendText(filepath))
{
sw.WriteLine(Message);
}
}
}
}
}
而不是打电话
global::MFTSearchService.Search.SearchStart();
在计时器中,您可以添加BackgroundWorker并在计时器中启动它。对于计时器上的每个滴答声,您可以检查Backgroundworker是否忙。如果您在运行BackgroundWorker之前检查它,它将在完成之前不会运行
private void OnElapsedTime(object source, ElapsedEventArgs e)
{
WriteToFile("Service is recall at " + DateTime.Now);
timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
timer.Interval = TimeSpan.FromMinutes(30).TotalMilliseconds; //number in milisecinds
timer.Enabled = true;
if (!backgroundWorker1.IsBusy())
{
backgroundWorker1.RunWorkerAsync();
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
global::MFTSearchService.Search.SearchStart();
}
后台工作线程不会锁定主线程,因此计时器将继续循环。因此,您可以随意设置时间间隔。与其使用轮询设计,为什么不在服务计划完成后再次使用服务计划本身,即递归设计?我尝试过这一方法,但不断出现错误。它不喜欢这两个背景