Timer in windows service

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2007
Posts: 23
Reputation: Wiizl is an unknown quantity at this point 
Solved Threads: 0
Wiizl Wiizl is offline Offline
Newbie Poster

Timer in windows service

 
0
  #1
Aug 12th, 2008
I'm trying to make basic windows service that should write into application event log some text once every minute. So I'm using a timer. But the Tick event doesn't fire.. I've tried all kinds of timer1.start() or timer1.enabled=true ... but nothing works. Everything with service itself is ok- I mean it can be installed, started and onStart() and onStop() it writes event log (also custom, not only the windows default, so the writing itself works..)
So here's the code:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Diagnostics;
  6. using System.ServiceProcess;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Timers;
  10. using System.Threading;
  11.  
  12. namespace SERVICE2
  13. {
  14. public partial class SERVICE2 : ServiceBase
  15. {
  16.  
  17. public SERVICE2()
  18. {
  19. InitializeComponent();
  20. }
  21.  
  22. protected override void OnStart(string[] args)
  23. {
  24. timer1.Enabled = true;
  25. string src = "Test_Service_2";
  26. string lg = "Application";
  27. string evnt = "SERVICE2 started!";
  28. if (!EventLog.SourceExists(src))
  29. EventLog.CreateEventSource(src, lg);
  30.  
  31. EventLog.WriteEntry(src, evnt);
  32. timer1.Start();
  33. }
  34.  
  35. protected override void OnStop()
  36. {
  37. string src2 = "Test_Service_2";
  38. string lg2 = "Application";
  39. string evnt2 = "SERVICE2 stopped!";
  40. if (!EventLog.SourceExists(src2))
  41. EventLog.CreateEventSource(src2, lg2);
  42.  
  43. EventLog.WriteEntry(src2, evnt2);
  44. timer1.Stop();
  45. timer1.Enabled = false;
  46. }
  47.  
  48. private void timer1_Tick(object sender, EventArgs e)
  49. {
  50. string sSource;
  51. string sLog;
  52. string sEvent;
  53.  
  54. sSource = "Test_Service_2";
  55. sLog = "Application";
  56. sEvent = "Timer TICK";
  57.  
  58. if (!EventLog.SourceExists(sSource))
  59. EventLog.CreateEventSource(sSource, sLog);
  60.  
  61. EventLog.WriteEntry(sSource, sEvent);
  62. EventLog.WriteEntry(sSource, sEvent,
  63. EventLogEntryType.Warning, 234);
  64. }
  65.  
  66. }
  67. }
I'd appreciate any help.
Could the problem be in timer properties suck as modifiers: public, or generate members- true?
I'm using Visual Studio 2005
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 39
Reputation: nvmobius is an unknown quantity at this point 
Solved Threads: 4
nvmobius nvmobius is offline Offline
Light Poster

Re: Timer in windows service

 
0
  #2
Aug 12th, 2008
I don't see where you Timer is declared. It's quite possible that it's losing scope and being garbage collected.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC