I wrote a program that makes use of the System.Timers.Timer name space and it is not working like expected it to.
My program listed bellow excepted two date values and determined if one date is greater than the other by using the DateTime.compare method. If one date is greater than the other, it will evaluate how much greater they are by calculating the time interval. I then convert that time interval into milliseconds and send that value to the Systems.Timer.Interval as coded bellow.
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Timers;
using System.Web.UI.HtmlControls;
/// <summary>
/// Summary description for recorder
/// </summary>
public class recorder
{
private string PID1;
DateTime startDateTime;
DateTime endDateTime;
double interval;
public recorder(string PID1, DateTime startDateTime, DateTime endDateTime, double Interval)
{
//this constructor init the data members for this class
this.PID1 = PID1;
this.startDateTime = startDateTime;
this.endDateTime = endDateTime;
this.interval = Interval;
}
public void startRecorder(/*string PID1, DateTime startDate, DateTime endDate*/)
{
//Using the class data members to calculate the dateTime diff.
DateTime current = DateTime.Now; //.Parse("10/12/2007 11:12:40");
int compareBegining = this.startDateTime.CompareTo(current); //startDate.CompareTo(current);
int compareEnding = this.endDateTime.CompareTo(current); // (endDate.CompareTo(current));
//if (BeginingInterval == 0 || BeginingInterval == -1)
if(compareBegining == 0 || compareBegining == -1)
{
try
{
//Response.Write("The interval is: " + compareBegining.ToString());
webServices start = new webServices();
start.LocateServerUPL(this.PID1, this.interval);
//start.SingleLocateMSAL(this.PID1);
if (compareEnding == 1)
{
Int32 endingTimeInSec = 0;
Int32 endingTimeInMilSec = 0;
TimeSpan b4UStop = this.endDateTime - current;
endingTimeInSec = Convert.ToInt32(b4UStop.TotalSeconds);
endingTimeInMilSec = Convert.ToInt32(b4UStop.TotalMilliseconds);
System.Timers.Timer time2Stop = new System.Timers.Timer();
time2Stop.AutoReset = false;
time2Stop.Enabled = true;
time2Stop.Interval = endingTimeInMilSec;
time2Stop.Elapsed += new ElapsedEventHandler(stopRecorder);
GC.KeepAlive(b4UStop);
}
}
catch (Exception end)
{
}
}
else if (compareBegining == 1)
{
try
{
Int32 timeInSec = 0;
Int32 timeInMilSec = 0;
//get the time diffence between the two dates
TimeSpan b4UStart = this.startDateTime - current;
timeInSec = Convert.ToInt32(b4UStart.TotalSeconds);
timeInMilSec = Convert.ToInt32(b4UStart.TotalMilliseconds);
System.Timers.Timer time2Start = new System.Timers.Timer();
time2Start.AutoReset = false;
time2Start.Enabled = true;
time2Start.Interval = timeInMilSec;
time2Start.Elapsed += new ElapsedEventHandler(startRecorder);
GC.KeepAlive(time2Start);
if (compareEnding == 1)
{
Int32 endingTimeInSec = 0;
Int32 endingTimeInMilSec = 0;
TimeSpan b4UStop = this.endDateTime - current;
endingTimeInSec = Convert.ToInt32(b4UStop.TotalSeconds);
endingTimeInMilSec = Convert.ToInt32(b4UStop.TotalMilliseconds);
System.Timers.Timer time2Stop = new System.Timers.Timer();
time2Stop.AutoReset = false;
time2Stop.Enabled = true;
time2Stop.Interval = endingTimeInMilSec;
time2Stop.Elapsed += new ElapsedEventHandler(stopRecorder);
GC.KeepAlive(b4UStop);
}
}
catch(Exception start)
{
}
}
}
private void startRecorder(object sender, ElapsedEventArgs e)
{
webServices start = new webServices();
start.LocateServerUPL(this.PID1, this.interval);
//Response.Write("Great job!");
}
private void stopRecorder(object sender, ElapsedEventArgs e)
{
webServices stop = new webServices();
stop.StopRecorder(this.PID1);
//Response.Write("2sec count");
}
}
If the value of time2Start and time2Stop interval falls with a span of 20 minutes everything works fine. However if the two intervals fall between half hour to 45 minutes, the time2Start interval would fire as expected but the time2Stop will not execute. Additionally, if both time2Start and time2Stop falls outside of 45 minutes none of them would fire at all.
I don’t understand what is really going on at the back-end and I would like to know how I could fix this problem. This site is my last chance to get this program to work like it should or I’m going to be standing at the unemployment line.
Pleeeeeeease.