Problem

You want to return a task result to the client.

Solution

Set the task's Result property with a user defined result object. The task's Result property is serialized and returned back to the client. The client can access the task's result using the Result property of the task after the task completes.

C#  Copy imageCopy
using System;
using AGI.Parallel.Client;
using AGI.Parallel.Infrastructure;

namespace CodeSamples
{
    class Program
    {
        static void Main(string[] args)
        {
            using (IJobScheduler scheduler = new ClusterJobScheduler("localhost"))
            {
                scheduler.Connect();
                Job job = scheduler.CreateJob();

                job.AddTask(new ReturnsSimplevalue());
                job.AddTask(new SetsValueOfProperties());
                job.AddTask(new ReturnsDotNetObject());

                job.Submit();
                job.WaitUntilDone();

                // Once the task completes, the task gets updated with the result object.
                Console.WriteLine("Simple Value: Result={0}", job.Tasks[0].Result);
                Console.WriteLine("Sets Property: A={0}, B={1}, C={2}", job.Tasks[1].Properties["A"], job.Tasks[1].Properties["B"], job.Tasks[1].Properties["C"]);
                Console.WriteLine("Complex Value: A={0}, B={1}", ((AnotherDotNetObject)job.Tasks[2].Result).A, ((AnotherDotNetObject)job.Tasks[2].Result).B);
           }

           /*
            * The output of the application should resemble:
            * Simple Value: Result=3
            * Sets Property: A=1, B=2, C=3.14
            * Complex Value: A=String, B=2/28/2013 3:06:18 PM
            */
        }

        [Serializable]
        class ReturnsSimplevalue : Task
        {
            public override void Execute()
            {
                this.Result = 3;
            }
        }

        [Serializable]
        class SetsValueOfProperties : Task
        {
            public override void Execute()
            {
                this.SetProperty("A", 1);
                this.SetProperty("B", "2");
                this.SetProperty("C", 3.14);
            }
        }

        [Serializable]
        class ReturnsDotNetObject : Task
        {
            public override void Execute()
            {
                this.Result = new AnotherDotNetObject
                    {
                        A = "String",
                        B = DateTime.Now
                    };
            }
        }

        [Serializable]
        class AnotherDotNetObject
        {
            public string A { get; set; }
            public DateTime B { get; set; }
        }
    }
}

Discussion

Returning a value after the task executes is necessary if the result needs processing. A task can return different types of results including user defined objects. To learn more about all of these options, read Working with task results.

See Also