Solved

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

Posted on 2007-12-04
7
215 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now