Skip to main content
Version: 8.2

Register a background operation

Level: intermediate
Example

Create a business process that registers a background operation. The background operation takes approximately 30 seconds. After this time passes, the process must add an Activity created by background task record to the list view of the Activities section list.

1. Create a class for the activity object

  1. Go to the Configuration section and select a user-made package to add the schema.

  2. Click AddSource code on the section list toolbar.

  3. Go to the Schema Designer and fill out the schema properties:

    • Set Code to "UsrActivityData."
    • Set Title to "ActivityData."

    Click Apply to apply the properties.

  4. Add the source code in the Schema Designer.

    UsrActivityData
    namespace Terrasoft.Configuration {
    using System;
    using Terrasoft.Common;
    using Terrasoft.Core;
    using Terrasoft.Core.DB;
    public class UsrActivityData {
    public string Title {
    get;
    set;
    }
    public Guid TypeId {
    get;
    set;
    }
    }
    }
  5. Publish the schema.

2. Create a class that adds an activity

  1. Go to the Configuration section and select a user-made package to add the schema.

  2. Click AddSource code on the section list toolbar.

  3. Go to the Schema Designer and fill out the schema properties:

    • Set Code to "UsrBackgroundActivityCreator."
    • Set Title to "BackgroundActivityCreator."

    Click Apply to apply the properties.

  4. Add the source code in the Schema Designer.

    UsrBackgroundActivityCreator
    namespace Terrasoft.Configuration {
    using System;
    using Terrasoft.Common;
    using Terrasoft.Core;
    using Terrasoft.Core.DB;
    using Terrasoft.Core.Tasks;
    using System.Threading.Tasks;

    public class UsrBackgroundActivityCreator: IBackgroundTask <UsrActivityData> , IUserConnectionRequired {
    private UserConnection _userConnection;

    /* Implement the Run method of the IBackgroundTask interface. */
    public void Run(UsrActivityData data) {
    /* Forced 30 second delay. */
    System.Threading.Tasks.Task.Delay(TimeSpan.FromSeconds(30));
    /* Create an activity. */
    var activity = new Activity(_userConnection) {
    UseAdminRights = false,
    Id = Guid.NewGuid(),
    TypeId = data.TypeId,
    Title = data.Title,

    /* Set the activity category to "To do." */
    ActivityCategoryId = new Guid("F51C4643-58E6-DF11-971B-001D60E938C6")
    };
    activity.SetDefColumnValues();
    activity.Save(false);
    }

    /* Implement the SetUserConnection method of the IUserConnectionRequired interface. */
    public void SetUserConnection(UserConnection userConnection) {
    _userConnection = userConnection;
    }
    }
    }

    The UsrBackgroundActivityCreator class implements the IBackgroundTask<UsrActivityData> and IUserConnectionRequired interfaces. After a forced 30 seconds delay, the Run() method creates an instance of the Activities section object based on the given UsrActivityData instance.

  5. Publish the schema.

3. Create a business process that starts the background operation

  1. Go to the Configuration section and select a user-made package to add the schema.

  2. Click AddBusiness process on the section list toolbar.

  3. Go to the Process Designer and fill out the process properties:

    • Set the Title property in the element setup area to "Background Task Example Process."
    • Set the Code property on the Settings tab of the element setup area to "UsrBackgroundTaskExampleProcess."
  4. Implement the business process.

    1. Click System actions in the Designer’s element area and place a Script task element between the Simple start event and Terminate end event on the Process Designer’s working area.

    2. Set the name of the Script task element to "Create background task."

    3. Add the code of the Script task element.

      Сode of the Script task element
      var data = new UsrActivityData {
      Title = "Activity created by background task",
      TypeId = ActivityConsts.TaskTypeUId
      };
      Terrasoft.Core.Tasks.Task.StartNewWithUserConnection <UsrBackgroundActivityCreator, UsrActivityData> (data);

      return true;
    4. Click the button in the Usings block on the Methods tab of the Process Designer and add the Terrasoft.Configuration namespace. This is required for the business process to support the implementations of the class for the activity object and class that adds an activity.

  5. Publish the changes.

Outcome of the example

To run the Background Task Example Process business process, click Run on the Process Designer’s toolbar.

As a result, the Background Task Example Process business process will add the Activity created by background task record to the list view of the Activities section list.


Resources

Package with example implementation