简体   繁体   中英

Sleep windows service until Thread timer starts

I'm creating a windows service to run some task withing intervals. 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.

But the problem is main thread dies before thread timer starts. So I tried to add Thread.CurrentThread.Join() and Sleep() until timer start. But after I install the windows service I cannot start the windows service, because sleep or block is in OnStart event. 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. Otherwise you're just implementing your own scheduling, with much less functionality to what is already built in to windows.

See Jon Galloway post on why not to use a service to run scheduled tasks.

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 .

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. It is in this thread that you will create your timers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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