简体   繁体   English

睡眠窗口服务直到线程计时器启动

[英]Sleep windows service until Thread timer starts

I'm creating a windows service to run some task withing intervals. 我正在创建一个Windows服务来运行一些间隔的任务。 Here, user can set the task start time (first run) and the task intervals. 在这里,用户可以设置任务开始时间(第一次运行)和任务间隔。 In my program in OnStart event, set the timer and wait for trigger. OnStart事件的程序中,设置计时器并等待触发器。

But the problem is main thread dies before thread timer starts. 但问题是线程计时器启动前主线程死了。 So I tried to add Thread.CurrentThread.Join() and Sleep() until timer start. 所以我尝试添加Thread.CurrentThread.Join()Sleep()直到计时器启动。 But after I install the windows service I cannot start the windows service, because sleep or block is in OnStart event. 但在我安装Windows服务后,我无法启动Windows服务,因为sleepblock是在OnStart事件中。 So it stuck, or sleep so long time and shows some exception. 所以它卡住了,或者睡了很长时间并且显示出一些异常。

Simply what I need is stop exit main thread untill Thread timer trigger. 我只需要停止退出主线程直到线程定时器触发器。

  public partial class Service1 : ServiceBase
  {
    TimerCallback timer_delegate;
    Timer houskeep_timer;

   protected override void OnStart(string[] args)
    {
      SettingReader settings = new SettingReader();
      if (settings.init())
      {
        timer_delegate = new TimerCallback(houseKeep);
        houskeep_timer = new Timer(timer_delegate, "testing", 33333100000, 44450000);
        //Thread.CurrentThread.Join();
      }
    }

     private void houseKeep(object setting_obj)
    {
        string message = (string)setting_obj;
        writeToLogFile(message);
    }   
}

I wouldn't use timers for this, I'd use a normal exe and set it up in the task scheduler. 我不会使用计时器,我会使用普通的exe并在任务调度程序中设置它。 Otherwise you're just implementing your own scheduling, with much less functionality to what is already built in to windows. 否则,您只是实现自己的调度,而内置于Windows中的功能要少得多。

See Jon Galloway post on why not to use a service to run scheduled tasks. 请参阅Jon Galloway的帖子 ,了解为何不使用服务来运行计划任务。

note that this is not the best way to do multithreading stuff, but it may be a solution to your problem. 请注意,这不是执行多线程处理的最佳方法,但它可能是解决您的问题的方法。
use a boolean variable that is global to the treads. 使用一个对于踏板是全局的布尔变量。 set it on main tread and look if it changes! 将它设置在主踏板上,看它是否发生变化! change it in the service tread when you wish the main thread to exit. 当您希望主线程退出时,在服务步骤中更改它。 after that the main thread will exit at the time you want. 之后主线程将在您想要的时间退出。 no need to do any invoke method or something as long as the flags you are creating in-between the treads are bool . 只要您在踏板之间创建的标志是bool就不需要执行任何invoke方法或其他操作。

This will achieve what you need 这将实现您的需求

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;

namespace WindowsService1
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }

        private System.Threading.AutoResetEvent are = new System.Threading.AutoResetEvent(false);

        protected override void OnStart(string[] args)
        {
            new System.Threading.Thread(mt) { IsBackground = true }.Start();
        }

        private void mt()
        {
            // Set up timer here

            // wait for OnStop indefinitely
            are.WaitOne();
        }

        protected override void OnStop()
        {
            are.Set();
        }
    }
}

The OnStart will start a thread that waits indefinitely for the OnStop. OnStart将启动一个无限期等待OnStop的线程。 It is in this thread that you will create your timers. 在这个帖子中,您将创建您的计时器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM