• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 254
  • Last Modified:

Service, Event, on the 5's minutes, HELP!

Hello all and thanks in advance, (this site has helped me so much on par..awesome!!)

Anyway....I am trying to create a service that will execute on the 5 minute mark. Instead of using a timer to run every 5 minutes, I want to execute my code when DateTime.localtime(modulus)5=0. That is... when dividing the minutes by 5, if the remainder is 0, execute code. Of course the code will not need to use modulus but this is the idea. I have writen a bulk of my code. The code telnets to a service, runs a series of keystrokes then downloads and strips off and stores information. As of now it is a simple exe running under a user and it works pretty reliably. I have yet to explore what it takes to turn this into a service but I think there is enough help here and C# corner to help me do that, what I am having trouble with is how to execute my code on the 5's.  The code takes anywhere from 1 to 2 minutes to run. I dont think that it would ever run more than 4 or 5 minutes, so overlaping would not be an issue. I have attached some code I pulled from c# corner plus some stuff i want to do. I dont know if I have put the code in the right place. Hopefully anyone who posts will let me know the error of my ways. Maximum points to the best helper.THANKS EVERYONE!!

 
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Threading;
using System.Net;
using System.Net.Sockets;
 
namespace SimpleService
{
	public class Service1 : System.ServiceProcess.ServiceBase
	{
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
	
		public Service1()
		{
			// This call is required by the Windows.Forms Component Designer.
			InitializeComponent();
 
			// TODO: Add any initialization after the InitComponent call
			
			//OK this is where I put my code?????
			//this is probably not the most elegant way to do this as I assume
			//this is a polling situation==high cpu util
			//is there a better way?
			while(true)
			{
				if(//time==5 minute mark)//this is the modulus 5 minute mark
				{
					//call function or insert code to do on the 5's
				}
				while(//time==5 minute mark)//I only want to run once in 5 minutes
				//if the code only takes 30 seconds it would run again unless I wait
				//for the 5 minute mark to pass
			}
		}
	
		// The main entry point for the process
		static void Main()
		{
			System.ServiceProcess.ServiceBase[] ServicesToRun;
	
			// More than one user Service may run within the same process. To add
			// another service to this process, change the following line to
			// create a second service object. For example,
			//
			// ServicesToRun = new System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()};
			//
			ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };
	
			System.ServiceProcess.ServiceBase.Run(ServicesToRun);
		}
 
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			components = new System.ComponentModel.Container();
			this.ServiceName = "Service1";
		}
 
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}
 
		/// <summary>
		/// Set things in motion so your service can do its work.
		/// </summary>
		protected override void OnStart(string[] args)
		{
			// TODO: Add code here to start your service.
			EventLog.WriteEntry("My simple service started.");
		}
	
		/// <summary>
		/// Stop this service.
		/// </summary>
		protected override void OnStop()
		{
			// TODO: Add code here to perform any tear-down necessary to stop your service.
		}
	}
 
}

Open in new window

0
jerryleeclark
Asked:
jerryleeclark
  • 3
  • 2
  • 2
1 Solution
 
surajgupthaCommented:
>> Yes its ok to put the code here
>> Yes you can sleep the while loop for 1 minute so it wakes up every minute to check to see if 5 minutes have passed since its last run
>> Why didnt you want to use a timer again?
0
 
surajgupthaCommented:
>>>> Yes you can sleep the while loop for 1 minute so it wakes up every minute to check to see if 5 minutes have passed since its last run

This is to stop the high CPU utilization.
0
 
Jaime OlivaresSoftware ArchitectCommented:
I would suggest:
                  

DateTime t = DateTime.Now.AddMinutes(5);
while(true)
{
    if (DateTime.Now > t)
    {
          t.AddMinutes(5);
          // do some task
    }
}

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Jaime OlivaresSoftware ArchitectCommented:
always is good to put a sleep function inside to avoid CPU load:

DateTime t = DateTime.Now.AddMinutes(5);
while(true)
{
    if (DateTime.Now > t)
    {
          t.AddMinutes(5);
          // do some task
    }
    System.Threading.Thread.Sleep(100);  // sleep 100 miliseconds
}
0
 
jerryleeclarkAuthor Commented:
Well, maybe I am incorrectly locked into thinking about the timer wrong. The problem with:        do task, wait for timer 5minutes, do task, wait for timer 5minutes....etc.    Is that the do task part could take 30 seconds or 2.5 minutes. That will put a time stamp on the task at varying times through the day. My intent is to be able to time stamp the task on the 5's   e.g.  13:45:00   13:50:00   13:55:00 etc.  I suppose if the timer was in its own thread, I could use it to trigger exactly five minutes apart and run the task, but 1) I dont know how to do that and 2) i still will not get the time stamp on the 5's.   I see how to do the AddMinutes(5), and I understand it but that method too will not have time stamps on the 5's. I was really hoping to do it on the 5's but I may have to pick a winner here if no other suggestions come along.
0
 
Jaime OlivaresSoftware ArchitectCommented:
OK, this will comply with your requirements, IMO:

DateTime t = DateTime.Now;
t = t.AddSeconds((5-t.Minute % 5)*60 - t.Second);   // fix the initial time to 5 min slot
while(true)
{
    if (DateTime.Now > t)
    {
          t = t.AddMinutes(5);  // add minutes exactly (there was a bug in previous version)
          // do some task
    }
    System.Threading.Thread.Sleep(100);  // sleep 100 miliseconds
}
           
0
 
jerryleeclarkAuthor Commented:
THANKS SO MUCH!!!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now