Hi,
Hi Ali,
I have analyzed the requirement and would like to share that its expected behavior of MSP as well that when number of tasks increases and we call CalculateProject, it takes longer time. You may verify this behavior by running the following macro which adds tasks and calls CalculateProject.
Sub TestMacro()
' Macro
Dim proj As Project
Dim tsk As task
Set proj = Application.ActiveProject
proj.NewTasksCreatedAsManual = False
Dim start As Date
start = Time
Dim t As task
For i = 1 To 500
Set t = proj.Tasks.Add("Task " & i)
t.start = DateAdd("d", i + 1, t.start)
Application.CalculateProject
t.Duration = i* 480
Application.CalculateProject
t.Finish = DateAdd("d", i + 1, t.start)
Application.CalculateProject
Next i
Debug.Print Format(Time - start, "hh:mm:ss")
End Sub
For better performance, it is suggested that you may add tasks manually by setting the Project.CalculateafterEdit = false and then call Recalculate. Following is a sample code which adds 20 tasks manually and then calls Recalculate while taking very short time. Could you please give a try to the following sample code and let us know your feedback?
Project prj = new Project();
prj.CalculateAfterEdit = false;
string TaskName;
// Create new stopwatch
Stopwatch stopwatch = new Stopwatch();
// Begin timing
stopwatch.Start();
for (int i = 0; i < 20; i++)
{
TaskName = "Task " + i;
Task tempTask = prj.AddTask(TaskName);
tempTask.Start = tempTask.ConstraintDate = DateTime.Now.Date + new TimeSpan(8, 0, 0);
tempTask.ConstraintType = ConstraintType.StartNoEarlierThan;
tempTask.Duration = new TimeSpan((i + 1) * 8, 0, 0);
tempTask.Finish = tempTask.Start.AddDays(i + 1);
tempTask.ManualStart = tempTask.Start;
tempTask.ManualDuration = tempTask.Duration;
tempTask.ManualFinish = tempTask.Finish;
tempTask.IsManual = true;
Console.WriteLine(i + " Start=" + tempTask.Start.ToString() + ", Finish=" + tempTask.Finish.ToString() + ", Duration=" + tempTask.Duration.ToString());
}
prj.UpdateReferences();
prj.CalcTaskIds();
prj.CalcTaskUids();
prj.CalcSlacks();
// Set early/late dates and slacks
Task.Recalculate(prj.RootTask);
// Stop timing
stopwatch.Stop();
// Write result
Console.WriteLine("Time elapsed: {0}", stopwatch.Elapsed);
//Recalculate(prj);
prj.Save(@“D:\Result.xml”, SaveFileFormat.XML);
Your solution has better performance but this issue is not happening for automatic tasks. When I insert 5000 automatic tasks it takes almost 900 ms to recalculate but when I insert 5000 manual tasks it takes more than 30 minutes to complete.
Hi Ali,