如何使Windows服务在重复过程之前完成所有代码

因此,我创建了一项在服务器上运行的服务,查看不同的文件夹共享,并将其放入数据库表中以进行自定义搜索,但是它提取了一个搜索位置的数据表并一次遍历它们,效果很好与小测试文件夹。现在,我尝试将其实际使用,但在计时器重新启动它之前,它无法通过第一个文件夹路径。我可以延长时间,但是我基本上希望它能够持续运行并在第一个完成后立即重新开始,或者不确定是否可以同时运行所有路径。我让它每30分钟运行一次,但肯定不够长。

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);
                }
            }
        }
    }
}
robinwu20090630 回答:如何使Windows服务在重复过程之前完成所有代码

代替打电话

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();
}

后台工作者不会锁定您的主线程,因此您的计时器将继续循环。因此,您可以随意设置任何间隔。

本文链接:https://www.f2er.com/3164317.html

大家都在问