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);
}
}
TimeSpan.FromHoursorTimeSpan.FromMinutesfor your arguments rather than milliseconds. - Jim Mischel