here i am using window service, using a logic as my service will work only once in the 24 hours by with the help of have configured in the app.config.
Ex : i will mention hour in app config as “10” so daily once my service will run by exactly 10 clock
But problem is when i start my service, it was throwing an error as 1053(Timely fashion error) and status it was showing as Starting in services.msc, no more start and Restart functions are not shown in the popup window of right click
wondering is it was done the job perfectly,by ten o clock exactly.
why it was not shown as started, why it was throwing an error?
i have pasted the sample code bellow and kindly advice whether if i did any wrong
on start method
protected override void OnStart(string[] args)
{
DateTime tenAM = DateTime.Today.AddHours(strSETHOST);
if (DateTime.Now > tenAM)
tenAM = tenAM.AddDays(1);
// calculate milliseconds until the next 10:00 AM.
int timeToFirstExecution = (int)tenAM.Subtract(DateTime.Now).TotalMilliseconds;
// calculate the number of milliseconds in 24 hours.
int timeBetweenCalls = (int)new TimeSpan(24, 0, 0).TotalMilliseconds;
TimerCallback methodToExecute = kickstart;
// start the timer. The timer will execute "ProcessFile" when the number of seconds between now and
// the next 10:00 AM elapse. After that, it will execute every 24 hours.
System.Threading.Timer timer = new System.Threading.Timer(methodToExecute, null, timeToFirstExecution, timeBetweenCalls);
Thread.Sleep(Timeout.Infinite);
}
protected override void OnStop()
{
}
public static void kickstart(object nowtime)
{
Service1 foo = new Service1();
foo.Startjob();
}
private void Startjob()
{
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew)) // Transaction Scope Started
{
if ((threadPURGE == null) || (threadPURGE.ThreadState == System.Threading.ThreadState.Stopped) || (threadPURGE.ThreadState == System.Threading.ThreadState.Unstarted) || (threadPURGE.ThreadState == System.Threading.ThreadState.Aborted))
{
threadPURGE = new Thread(new ThreadStart(DynamicThreadGen)); // Thread Initialize for ITD
}
try
{
if ((threadPURGE == null) || (threadPURGE.ThreadState == System.Threading.ThreadState.Stopped) || (threadPURGE.ThreadState == System.Threading.ThreadState.Unstarted) || (threadPURGE.ThreadState == System.Threading.ThreadState.Aborted))
{
threadPURGE.Start(); // Thread Started for ITD
}
}
catch (Exception ex)
{
string err = ex.Message.ToString();
}
finally
{
scope.Complete();
}
}
}
private void DynamicThreadGen()
{
/// service work
}
You need to allow your
OnStartmethod to complete within a Windows allocated timeout, otherwise Windows can’t tell that it has started. Hold on to theTimerin a class field so it does not get garbage collected and disposed, then ditch theThread.Sleep