Windows service Timer ?

  • Thread starter Thread starter Vamshi K J
  • Start date Start date
V

Vamshi K J

Guest
Hi,

I have a Windows Service which should run daily at 10:30 am and 11:00 am to process the logic ( process and create 20 txt files) per shift (i.e. for the two shifts ). Below is the code which triggers Windows Service to run every 30 minutes once it is started.

Example: Based on shift end times (refer the table screenshot) If the Windows service is started at 10:00 am then the next refresh time is 10:30 am.........

1565583.png


But while testing i have observed an issue. Need inputs to solve it..

Issue:
Suppose If the windows service is started at 11:15 am instead of 10:00 am then the next refresh time is 11:45 pm which will not execute the logic at 1st shift end time for the day which is 10:30 am and followed by next refresh time will be 12:15 pm which will not execute the logic at 2nd shift end time as well which is 11:00 am. Need inputs to handle this issue?

namespace Service
{
partial class WindowsService : ServiceBase
{
private System.Timers.Timer RefreshTimer = null;

public Data_Service()
{
InitializeComponent();
}

protected override void OnStart(string[] args)
{
try
{
int refreshTimeInterval = 1800000; //Default Time interval set in milliseconds (i.e. 60000 ms = 1 minute)

this.RefreshTimer = new System.Timers.Timer();
this.RefreshTimer.Interval = refreshTimeInterval;

this.RefreshTimer.Elapsed += RefreshTimer_Elapsed;
this.RefreshTimer.Start();
}
catch (Exception ex)
{
throw ex;

}
finally
{

}
}


protected override void OnStop()
{
if (this.RefreshTimer != null)
{ this.RefreshTimer.Dispose(); }
}


private void RefreshTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
string errorMessage = string.Empty;
try
{
if (this.RefreshTimer != null)
{ this.RefreshTimer.Stop(); }

DataAccess DA = new DataAccess();
IList<ShiftModel> shiftModel = null;
shiftModel = DA.GetShiftEndTime();

if (shiftModel != null && shiftModel.Count > 0)
{
foreach (var shift in shiftModel)
{
if (shift.ShiftEndTime.Trim() != "")
{

// Convert the DateTime to EST TimeZone
var timeToConvert = DateTime.Now;
var est = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
var targetTime = TimeZoneInfo.ConvertTime(timeToConvert, est).ToString("HH:mm");
var currentDateTime = DateTime.ParseExact(targetTime, "HH:mm", null, System.Globalization.DateTimeStyles.None);

var shiftDateTime = DateTime.ParseExact(shift.ShiftEndTime.Trim(), "HH:mm", null, System.Globalization.DateTimeStyles.None);
var EndDateTime = shiftDateTime.AddMinutes(30);

if (currentDateTime == shiftDateTime || (currentDateTime >= shiftDateTime && currentDateTime <= EndDateTime))
{
// logic here
}

}
else
{
errorMessage = string.Format("Shift end time is null or empty.");
LogManager.ApplicationLogger.Error(errorMessage);
return;
}
}
}
else
{
errorMessage = string.Format("Shift end time is null or empty.");
LogManager.ApplicationLogger.Error(errorMessage);
}
}
catch (Exception ex)
{
LogManager.ApplicationLogger.Error(Constants.LOG_SEPARATOR + Constants.EXCEPTIONRAISED + ex.Message + Constants.LOG_SEPARATOR + ex.Source + Constants.LOG_SEPARATOR + ex.InnerException + Constants.LOG_SEPARATOR + ex.StackTrace, ex);
}
finally
{
if (this.RefreshTimer != null)
{ this.RefreshTimer.Start(); }
}
}

}
}


Thanks.



Vamshi Janagama

Continue reading...
 
Back
Top