Creating Tasks
A project is essentially composed of multiple tasks. Each task represents a unit of work that must be completed for the overall project to succeed. When working with project files, adding tasks is a fundamental activity.
Tasks may:
- Have dependencies on other tasks.
- Include subtasks (children).
- Have a defined duration.
With Aspose.Tasks for .NET, developers can create tasks programmatically and specify their name, hierarchy, or duration right at the time of creation.
Creating Tasks
The
Task class exposes the Add method, which allows adding a task to a project’s RootTask or as a child of another task. The default constructor creates a Task
instance without requiring any parameters. Developers can then configure its properties such as Name
, Duration
, or Start
.
Example: Adding a New Task
The following code sample demonstrates how to create new tasks and add them to a project:
1Project project = new Project();
2
3// Add task, sub task and save project
4Task task = project.RootTask.Children.Add("Summary1");
5Task subtask = task.Children.Add("Subtask1");
6project.Save("CreateTasks_out.xml", SaveFileFormat.XML);
Key Notes
- Newly created tasks are automatically included in the project’s task hierarchy.
- A task can be added directly to the RootTask or nested under another task to represent sub-tasks.
- After creation, additional properties such as resources, constraints, and calendars can be set.
FAQ
Q: Can I assign a duration when creating a task?
- Yes, after creating a task you can set the
Duration
property.
Q: Can tasks be created without linking to others?
- Yes. Dependencies are optional and can be added later with
TaskLink
.
Q: Does creating a task automatically set its start and finish dates?
- By default, the project calendar and scheduling rules are used to assign start and finish dates, but these can be overridden.