tags:

views:

334

answers:

2

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?

+2  A: 

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#?

Jon Skeet
You've caught me! I'm new to c#, with a background in Java. I'll mock up a small program to see if I can replicate this bug elsewhere.
Mike
Just copy/paste my example and you're half way there.
Magnus Johansson
Thanks for the help, I added logging and observed the results I was expecting. I removed the logging and still saw the results expected. I have no idea what this was or where it came from but the problem is rectified. Glitch in the matrix?
Mike
"Glitch in the matrix", or just an ordinary day... ;)
Magnus Johansson
+5  A: 

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.

Magnus Johansson
Thanks for this. I can confirm that this sample program works as I wish my method would! :)Back to head-scratching.
Mike
You're effort was appreciated. I used this code to help me solve the problem. For which I still do not really know what caused it! Regardless I am a firm believer of effort = reward, which is why I am accepting this as my answer. Thank you.
Mike