|
-
Mar 12th, 2010, 08:49 AM
#1
Thread Starter
Hyperactive Member
Do timers work in services?
Do timers not work in services? I have a service that is running fine, has been for a while. I'm trying to add some work that needs to kick off regularly, so I create a timer that fires every 5-seconds and kick it off when needed.
The Tick event never fires. Never, not once.
I'm not sure why it doesn't work. Here's the relevant code I'm using. I have 4 backgroundworker threads named, simply enough, bgw1 - bgw4. I wil show you the code for one of the threads so you can see what I'm doing.
Code:
namespace PolicyMigrationService
{
public partial class Service1 : ServiceBase
{
private Timer TimerThrd1;
protected override void OnStart(string[] args)
{
base.OnStart(args);
// Create the timers
TimerThrd1 = new Timer();
TimerThrd1.Interval = 5000;
TimerThrd1.Tick += new EventHandler(TimerThrd1_Tick);
backgroundWorker1.RunWorkerAsync();
backgroundWorker2.RunWorkerAsync();
backgroundWorker3.RunWorkerAsync();
backgroundWorker4.RunWorkerAsync();
}
private void bgw1_DoWork(object sender, DoWorkEventArgs e)
{
Process200Policies();
}
private void bgw1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (NextPolicyIndex < PolicyCount)
{
int load = CpuLoad.Query();
if (load > 90)
{
// Processor full, kick off timer to detect when it is empty.
TimerThrd1.Start();
return;
}
// Processor not full, kick off thread to process more
backgroundWorker1.RunWorkerAsync();
}
}
void TimerThrd1_Tick(object sender, EventArgs e)
{
LogMsg("Timer Tick...", 1);
int load = CpuLoad.Query();
if (load <= 90)
{
// Processor not full, start the thread to process the next chunk
bgw1.RunWorkerAsync();
TimerThrd1.Stop();
}
else
{
// Processor full, do nothing
}
}
}
}
So when the thread ends, I check the processor to see if it can handle more work. If not, then I kick of a timer that is supposed to fire every 5-seconds and do the same thing.
Only the tick event never fires. Does anybody have any idea why?
Last edited by HongKongCV; Mar 12th, 2010 at 08:51 AM.
Reason: Placed code in code block
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|