[Mono-dev] Task class usage problem

vart artemyv at gmail.com
Mon Mar 21 06:37:36 EDT 2011


I'm compiling the following code on VS2010 or Monodev 2.6 for windows with
Net4.0 settings and get the correct output

I compile the same code on Ubuntu/OpenSUSE using Mono 2.10 and Monodev 2.6
beta1 - I got not output.

What is the problem here?


static void Main()
    {

        var parent = Task.Factory.StartNew(() =>
        {
            // We’ll throw 3 exceptions at once using 3 child tasks:

            int[] numbers = { 0 };

            var childFactory = new TaskFactory
             (TaskCreationOptions.AttachedToParent,
TaskContinuationOptions.None);

            childFactory.StartNew(() => 5 / numbers[0]);   // Division by
zero
            childFactory.StartNew(() => numbers[1]);      // Index out of
range
            childFactory.StartNew(() => { throw null; });  // Null reference
        });

        try { parent.Wait(); }
        catch (AggregateException aex)
        {
            aex.Flatten().Handle(ex =>   // Note that we still need to call
Flatten
            {
                if (ex is DivideByZeroException)
                {
                    Console.WriteLine("Divide by zero");
                    return true;                           // This exception
is "handled"
                }
                else if (ex is IndexOutOfRangeException)
                {
                    Console.WriteLine("Index out of range");
                    return true;                           // This exception
is "handled"   
                }
                else
                {
                    Console.WriteLine("Other exception: " + ex.Message);
                    return true;                           // This exception
is "handled"   
                }
                //return false;    // All other exceptions will get rethrown
            });
        }
    }


Output on Windows

Other exception: Object reference not set to an instance of an object.
Divide by zero
Index out of range

These line could be shown in different order depending on Task execution
order.

On linux I do not get any output.
Same problem with several other samples using Task/TaskFactory classes from
System.Threading.Tasks namespace



--
View this message in context: http://mono.1490590.n4.nabble.com/Task-class-usage-problem-tp3393138p3393138.html
Sent from the Mono - Dev mailing list archive at Nabble.com.


More information about the Mono-devel-list mailing list