Reputation: 6008
I've been scratching my head over this one for a couple hours now, I've drafted in co-workers and we are all lost. This could be a case of too much coffee from the new espresso machine, or the fact it's Friday... We're not sure!
I have the following method:
private void calcuateEstimatedExecutionTimesForDueJobs(List<TestJob> dueJobs)
{
DateTime rollingTime = DatabaseConnection.getNow();
foreach (TestJob job in dueJobs)
{
job.setEstimatedStart(rollingTime);
double estimatedRuntime = job.getEstimatedRuntime();
rollingTime = rollingTime.AddSeconds(estimatedRuntime);
job.setEstimatedFinish(rollingTime);
}
}
The intention is to process a list of "TestJobs" which our app is queued to deliver. Our TestJob kindly knows how long it will "probably" take to run so I hope to use this information here to predict the "Start" & "Finish" times of each TestJob.
Unfortunately, rollingTime is never altered. Although job.getEstimatedRuntime() always returns a positive double, calling AddSeconds() on the current TestJob and passing this value has no effect.
Is there a bug in my code, or something more sinister?
Update: I noticed that this question is still getting a few views. For those of you suffering an odd issue like mine, I remember resolving this one by simply restarting Visual Studio / Rebooting. I guess this issue still crops up from time to time... (Ahem...)!
Upvotes: 1
Views: 14736
Reputation: 81
OBS: ref https://msdn.microsoft.com/en-us/library/system.datetime.addseconds(v=vs.110).aspx
This method does not change the value of this DateTime. Instead, it returns a new DateTime whose value is the result of this operation.
Upvotes: 8
Reputation: 28325
Creating a small test program based on your code:
public class Program
{
public static void Main(string[] args)
{
List<TestJob> jobList = new List<TestJob>();
jobList.Add(new TestJob() { ID = 1 });
jobList.Add(new TestJob() { ID = 2 });
jobList.Add(new TestJob() { ID = 3 });
jobList.Add(new TestJob() { ID = 4 });
CalcuateEstimatedExecutionTimesForDueJobs(jobList);
foreach (TestJob job in jobList)
{
Console.WriteLine("{0} {1} {2}", job.ID, job.StartDate, job.FinishedDate);
}
Console.ReadLine();
}
private static void CalcuateEstimatedExecutionTimesForDueJobs(List<TestJob> dueJobs)
{
DateTime rollingTime = DateTime.Now;
foreach (TestJob job in dueJobs)
{
job.SetEstimatedStart(rollingTime);
double estimatedRuntime = job.GetEstimatedRuntime();
rollingTime = rollingTime.AddSeconds(estimatedRuntime);
job.SetEstimatedFinish(rollingTime);
}
}
}
public class TestJob
{
public int ID { get; set; }
public DateTime StartDate { get; set; }
public DateTime FinishedDate { get; set; }
public void SetEstimatedStart(DateTime date)
{
this.StartDate = date;
}
public void SetEstimatedFinish(DateTime date)
{
this.FinishedDate = date;
}
public double GetEstimatedRuntime()
{
return 42; //Answer to Life, the Universe, and Everything
}
}
Reveals that everything works as expected. The console output is:
1 02.10.2009 17:08:43 02.10.2009 17:09:25
2 02.10.2009 17:09:25 02.10.2009 17:10:07
3 02.10.2009 17:10:07 02.10.2009 17:10:49
4 02.10.2009 17:10:49 02.10.2009 17:11:31
Which is, as far as I can tell correct. Please double check your dependent code in your TestJob class and debug / log everything.
Upvotes: 5
Reputation: 1500675
That should work without any problems.
I suggest you try to convert the question into a short but complete program which demonstrates the problem. You might also want to add some logging in - log rollingTime
and
estimatedRuntime
on each iteration.
Btw, any reason why you're using Java conventions in C#?
Upvotes: 2