I've added a timer to my C# application which is running as a service. Everything seems to work fine but the timer never ticks! I've tried setting it to various intervals to see if it does work but nothing! I've also added a writelog even in the tick to see if it works or not. It does not.
Any ideas?
Code:for (int i;;) { cout << "Smith"; }
Are you using the standard Timer as in, the timer that you install on form applications?
DirkFirst Tutorials | Linux Forum
You can't use the stand GUI timer with a service. Use system.timer or thread.timer to accomplish this.
How do I use the system.timer or thread.timer? I have no idea how to make it tick.
Code:for (int i;;) { cout << "Smith"; }
I've got code that does this. I'll paste it when I go back to work.
I've got some code as well. Here is how to use it:
Declare it in your class
If you are doing a timer, you should use a thread that executes a function which then enables the timer. I'll skip that part though.Code:public partial class Service1 : ServiceBase { // Create a threaded timer private static System.Timers.Timer t = new System.Timers.Timer();
In your function:
Your Tick Function:Code:// Set the interval in MS for the timer t.Interval = 30000; // 30 seconds // TODO: Add code here to start your service. //Enable the timer and set the interval for 10 seconds t.Enabled = true; //Assign a new event handler to the Elapsed method t.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Tick); //Start the timer t.Start();
Code:private void timer1_Tick(object sender, System.Timers.ElapsedEventArgs e) { // Stop the Timer t.Stop(); t.Enabled = false; // Execute the function myFunction(); // Enable the timer t.Start(); t.Enabled = true; }
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks