Issue with Multiple Task links

Adding multiple tasklinks (which is pretty standard in mpps) doesn’t seem to work. In one project it when I save the writer throws an exception when a task has multiple task links
(Writing tasks fixed data finished.\r\nWriting tasks variable data started.\r\nWriting task baselines started.\r\nWriting task baselines finished.\r\nWriting tasks variable data finished.\r\nTask with UID=‘4’ was written successfully.\r\nWriting project’s tasks finished.\r\nWriting project’s tasks finished.\r\nWriting task links started.\r\n")

And just creating an
t System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)

I’m having another issue when opening a test project mpp file and then adding task links.
I receive this message
"Cannot link predecessor task to itself."

When running any of this test code at any of the new TaskLink lines. I tried adding the test task to the roottask children or an existing task on the root.

Task task1 = new Task(“ntask task”);
task1.Start = DateTime.Today;
task1.Duration = TimeSpan.FromDays(3);
baseProj.RootTask.Children[0].Children.Add(task1);

Task task22 = new Task(“Second task”);
task22.Start = DateTime.Today.AddDays(10);
task22.Duration = TimeSpan.FromDays(3);
baseProj.RootTask.Children[0].Children.Add(task22);

Task task33 = new Task(“3 task”);
task33.Start = DateTime.Today.AddDays(20);
task33.Duration = TimeSpan.FromDays(3);
baseProj.RootTask.Children[0].Children.Add(task33);


Task task44 = new Task(“4 task”);
task44.Start = DateTime.Today.AddDays(306);
task44.Duration = TimeSpan.FromDays(3);
baseProj.RootTask.Children[0].Children.Add(task33);

baseProj.UpdateReferences();
baseProj.CalcTaskIds();
//baseProj.CalcTaskUids();


TaskLink tsklnk1 = new TaskLink(task1, task44, TaskLinkType.FinishToStart);
baseProj.TaskLinks.Add(tsklnk1);
TaskLink tsklnk2 = new TaskLink(task22, task1, TaskLinkType.FinishToStart);
baseProj.TaskLinks.Add(tsklnk2);

TaskLink tsklnk3 = new TaskLink(task22, task33, TaskLinkType.FinishToStart);
baseProj.TaskLinks.Add(tsklnk3);
TaskLink tsklnk4 = new TaskLink(task33, task1, TaskLinkType.FinishToStart);

baseProj.TaskLinks.Add(tsklnk4);

Hi,

Thanks for writing to Aspose.Tasks support team.

I have analyzed your code and found that there are few mistakes as follows:

jj@inveniosoft.com:

baseProj.RootTask.Children[0].Children.Add(task1);

I modified this code assuming that RootTask has no children. So all the similar lines of code are modified as follows:

baseProj.RootTask.Children.Add(task1);

jj@inveniosoft.com:

Task task44 = new Task(“4 task”);
task44.Start = DateTime.Today.AddDays(306);
task44.Duration = TimeSpan.FromDays(3);

baseProj.RootTask.Children[0].Children.Add(task33);

Here in the last line task44 should be added rather than task33.

Following is the modified code along with a recalculating function containing all the necessary Calc functions.

static void IssueWithMultipleTaskLinks()
{
    Project baseProj = new Project();
    Task task1 = new Task("ntask task");
    task1.Start = DateTime.Today;
    task1.Duration = TimeSpan.FromDays(3);
    baseProj.RootTask.Children.Add(task1);
    Task task22 = new Task("Second task");
    task22.Start = DateTime.Today.AddDays(10);
    task22.Duration = TimeSpan.FromDays(3);
    baseProj.RootTask.Children.Add(task22);
    Task task33 = new Task("3 task");
    task33.Start = DateTime.Today.AddDays(20);
    task33.Duration = TimeSpan.FromDays(3);
    baseProj.RootTask.Children.Add(task33);
    Task task44 = new Task("4 task");
    task44.Start = DateTime.Today.AddDays(306);
    task44.Duration = TimeSpan.FromDays(3);
    baseProj.RootTask.Children.Add(task44);
    TaskLink tsklnk1 = new TaskLink(task1, task44, TaskLinkType.FinishToStart);
    baseProj.TaskLinks.Add(tsklnk1);
    TaskLink tsklnk2 = new TaskLink(task22, task1, TaskLinkType.FinishToStart);
    baseProj.TaskLinks.Add(tsklnk2);
    TaskLink tsklnk3 = new TaskLink(task22, task33, TaskLinkType.FinishToStart);
    baseProj.TaskLinks.Add(tsklnk3);
    TaskLink tsklnk4 = new TaskLink(task33, task1, TaskLinkType.FinishToStart);
    baseProj.TaskLinks.Add(tsklnk4);

    Recalculate(baseProj);

    baseProj.Save(@"D:\Aspose\Test.xml", SaveFileFormat.XML);
}

private static void Recalculate(Project project)
{
    project.CalcTaskIds();
    project.CalcTaskUids();
    project.CalcResourceIds();
    project.CalcResourceUids();
    project.CalcResourceAssignmentIds();
    project.CalcResourceAssignmentUids();
    project.CalcCalendarUids();
    project.UpdateReferences();
}

Could you please give it a try using latest version Aspose.Tasks for .NET 6.2.0 and let us know your feedback? We will be glad to provide you assistance.

Looks like the main issue was calling update references or recalulating before adding the task links. I was able update the mpp multiple tasklinks my test project after moving that, so I’ll investigate where i’m updating references in the live project. Thanks.

Hi,


Thank you for providing the feedback.

One issue was also about the addition of Task33 instead of Task44 in your sample code. Anyways, please feel free to write to us in case you have any additional query/inquiry in this regard.