943,669 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 14276
  • C# RSS
Aug 12th, 2008
0

Timer in windows service

Expand Post »
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:
C# Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
Wiizl is offline Offline
25 posts
since Jan 2007
Aug 12th, 2008
0

Re: Timer in windows service

I don't see where you Timer is declared. It's quite possible that it's losing scope and being garbage collected.
Reputation Points: 11
Solved Threads: 4
Light Poster
nvmobius is offline Offline
39 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Help Please
Next Thread in C# Forum Timeline: word a hyperlink then appear menus !!! how??





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC