ServiceTimer won't fire

  • Thread starter Thread starter Thirsty Traveler
  • Start date Start date
T

Thirsty Traveler

I dropped a Timer object onto a Windows Service and set enabled=true...
however, it does not appear to be firing. Do I need to do something else?
 
You probably have to set the Interval value too.

I have the same problem.

Working in C++, my InitializeComponent() has

this->ServiceTimer->Enabled = true;
this->ServiceTimer->Interval = 1000;
this->ServiceTimer->Tick += gcnew System::EventHandler(this,
&EMuxServiceWinService::ServiceTimer_Tick);

My OnStart() has

ServiceTimer->Start();

and my OnStop() has

ServiceTimer->Stop();

My OnStart, OnStop, and ServiceTimer_Tick all have code to write to a
log file. The OnStart and OnStart logs get written but the timer tick
log never does.

I confess to being a complete newbie at DotNet code, although I am
rather experienced at MFC and Windows32 programming in general, so I
might be missing something. But what could it be?

Note: I have also tried putting the timer enable and interval set in
OnStart.
 
That is pretty much what I have done as well... but the thing won't start.

I have taken the liberty of "correcting" your top-posting.

Since I am new to DotNet I don't understand what is under the hood.
But I was going to write my service in good old-fashioned Win32 code
and, for that, I know that I need a window (albeit invisible for a
service) and a message pump. My service also includes a SessionSwitch
event processer which never gets signalled.

Could it be that we have to add a MessageQueue or something like it to
the service?
 
I have taken the liberty of "correcting" your top-posting.

Since I am new to DotNet I don't understand what is under the hood.
But I was going to write my service in good old-fashioned Win32 code
and, for that, I know that I need a window (albeit invisible for a
service) and a message pump. My service also includes a SessionSwitch
event processer which never gets signalled.

Could it be that we have to add a MessageQueue or something like it to
the service?

OK, MessageQueue is not the right term -- I just saw the words and
assumed it would be a "Windows message pump" but I was wrong. And I
found and changed the property
this->CanHandleSessionChangeEvent = true;
but the service still doesn't respond to session change events.
 
Is top-posting the standard here? Strange!

Thanks much for that information. It seems to be exactly what I (we)
need. That was going to be my next try -- put the timers in the
equivalent of a worker thread that does have the equivalent of a
message pump! I'll bet my session-change detector will work inside a
worker thread, also.
 
There appears to be a bug in the Timer component provided in the toolbox
when dropped onto a service. I deleted this and used the following, which
works.

using System.Threading;

{
public partial class MyService : ServiceBase
{

TimerCallback tcb = null;
Timer timer = null;

protected override void OnStart(string[] args)
{
tcb = new TimerCallback(this.TimerCallback_Tick);
timer = new Timer(tcb, null, 0, 10000);
}

private void TimerCallback_Tick(object state)
{
//Do something
}
}
 
Many thanks to both you and to Greg Young for suggesting a system
threading timer. I just avoided the whole thing by putting a
ServiceWorker in the service. It is trivial to put it into a sleep
loop to simulate a timer. Either the worker can implement the timer
tick function or it can signal to the main service through the
progress method. It is very simple and effective.
 
Back
Top