Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2007-12-04
7
Medium Priority
?
247 Views
Last Modified: 2010-04-21
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
Comment
Question by:jerryleeclark
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 21

Expert Comment

by:surajguptha
ID: 20404517
>> 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
 
LVL 21

Expert Comment

by:surajguptha
ID: 20404519
>>>> 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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20404872
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20404897
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
 

Author Comment

by:jerryleeclark
ID: 20405897
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
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 2000 total points
ID: 20406198
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
 

Author Closing Comment

by:jerryleeclark
ID: 31412620
THANKS SO MUCH!!!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question