Solved

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

Posted on 2007-12-04
7
210 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
Comment Utility
>> 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
Comment Utility
>>>> 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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
THANKS SO MUCH!!!
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
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.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

763 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

12 Experts available now in Live!

Get 1:1 Help Now