Thomas
Thomas

Reputation: 34188

Regarding running multiple task & report when complete

i was looking for way out run multiple task and report about task status not sequencially. here i am pasting a code where multiple task running and reporting when all task complete.

var task1 = Task.Factory.StartNew(() =>
            {
                Thread.Sleep(1000);
                return "dummy value 1";
            });

            var task2 = Task.Factory.StartNew(() =>
            {
                Thread.Sleep(13000);
                return "dummy value 2";
            });

            var task3 = Task.Factory.StartNew(() =>
            {
                Thread.Sleep(2000);
                return "dummy value 3";
            });

            Task.Factory.ContinueWhenAll(new[] { task1, task2, task3 }, tasks =>
            {
                foreach (Task<string> task in tasks)
                {
                    Console.WriteLine(task.Result);
                }
            });
            Console.ReadLine();

task1 will take less time and then task3 will complete and last task2 will complete but ContinueWhenAll always showing task1 is completed and then task2 & task3. i want to modify the code in such way as a result which task complete fast that will show first not sequentially. where to change in code. guide please. thanks

UPDATE

var taskp = Task.Factory.StartNew(() =>
            {
                var task1 = Task.Factory.StartNew(() =>
                {
                    Thread.Sleep(1000);
                    return "Task1 finished";
                }).ContinueWith((continuation) => { Console.WriteLine(continuation.Result); });

                var task2 = Task.Factory.StartNew(() =>
                {
                    Thread.Sleep(3000);
                    return "Task2 finished";
                }).ContinueWith((continuation) => { Console.WriteLine(continuation.Result); });

                var task3 = Task.Factory.StartNew(() =>
                {
                    Thread.Sleep(2000);
                    return "Task3 finished";
                }).ContinueWith((continuation) => { Console.WriteLine(continuation.Result); });

                return "Main Task finished";
            });


            taskp.ContinueWith(t => Console.WriteLine(t.Result));
            Console.ReadLine();

Upvotes: 0

Views: 241

Answers (1)

Alex
Alex

Reputation: 23300

An easy solution would be to ContinueWith each task, individually. This is a very quick and dirty example:

var taskp = Task.Factory.StartNew(() =>
{
    var task1 = Task.Factory.StartNew(() =>
    {
        Thread.Sleep(1000);
        return "dummy value 1";
    }).ContinueWith((continuation) => { Console.WriteLine("task1"); });

    var task2 = Task.Factory.StartNew(() =>
    {
        Thread.Sleep(3000);
        return "dummy value 2";
    }).ContinueWith((continuation) => { Console.WriteLine("task2"); });

    var task3 = Task.Factory.StartNew(() =>
    {
        Thread.Sleep(2000);
        return "dummy value 3";
    }).ContinueWith((continuation) => { Console.WriteLine("task3"); });

    Task.Factory.ContinueWhenAll(new Task[] { task1, task2, task3}, (x) => { Console.WriteLine("Main Task Complete"); });
});            

The code is updated to adapt to OP's UPDATE, with a "Main" task which will output after all the "inner" tasks are complete. To make it so taskp returns the string instead of writing it, the code becomes

var taskp = Task.Factory.StartNew<string>(() =>
{
    var task1 = ...; //same
    var task2 = ...; //same
    var task3 = ...; //same
    return "Main Task Complete";

}).ContinueWith((x)=> Console.WriteLine(x.Result));

The StartNew<> overload must be used to specify the return type for the Task to have a Result.

Upvotes: 1

Related Questions