locked
Task error RRS feed

  • Question

  • User-776477372 posted

    Hi friend

    i am using Parallel Invoke. here two method MethodInvoke1 and MethodInvoke2.

     MethodInvoke1 has httprequest and MethodInvoke2 has httprequest.

    namespace ConsoleApplication1
    {
    class ParrelTest5
    {
    static void Main(string[] args)
    {
    PInvokeCancelExample();
    }

    static public void PInvokeCancelExample()
    {
    CancellationTokenSource cts = new CancellationTokenSource();
    ParallelOptions settings = new ParallelOptions() { CancellationToken = cts.Token };
    System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
    settings.MaxDegreeOfParallelism = System.Environment.ProcessorCount;
    sw.Start();
    Task.Factory.StartNew(() =>
    {
    Console.WriteLine("Cancellation in 20 sec.");
    Thread.Sleep(20000);
    settings.CancellationToken.Register(() => cancelNotification());
    cts.Cancel();
    Console.WriteLine("Canceled requested");
    });
    Parallel.Invoke(settings, () => MethodInvoke1(settings.CancellationToken),
    () => MethodInvoke2(settings.CancellationToken));

    sw.Stop();
    Console.WriteLine("Total Time" + sw.Elapsed.Seconds);
    Console.WriteLine("Ready");
    Console.ReadLine();
    }

    static private void MethodInvoke1(CancellationToken token)
    {
    while (!token.IsCancellationRequested)
    {
    Console.WriteLine("Running. Task ID: {0}", Task.CurrentId);
    Thread.Sleep(500);
    }
    }
    static private void MethodInvoke2(CancellationToken token)
    {
    while (!token.IsCancellationRequested)
    {
    Console.WriteLine("Running. Task ID: {0}", Task.CurrentId);
    Thread.Sleep(60000);
    }
    }
    static void cancelNotification()
    {

    }
    }
    }

    --------------------------------------

    Error Message

    Message :One or more errors occurred.
    Source :mscorlib
    StackTrace : at System.Threading.Tasks.Task.FastWaitAll(Task[] tasks) at System.Threading.Tasks.Parallel.Invoke(ParallelOptions parallelOptions, Action[] actions) at System.Threading.Tasks.Parallel.Invoke(Action[] actions) at MethodInvoke2()
    InnerExceptionMessage :Object reference not set to an instance of an object.

    InnerExceptionStackTrace : at ConsoleApplication1.PInvokeCancelExample.<>c__DisplayClass17.<request_amadeus_worldspan>b__10()

                                         at System.Threading.Tasks.Task.InnerInvoke() at System.Threading.Tasks.Task.Execute()

    Friday, December 20, 2013 3:43 AM

All replies

  • User197322208 posted

    catch aggregateexception and display exceptions

    Friday, December 20, 2013 7:06 AM
  • User-417640953 posted

     

    sarvesh.mca@hotmail.com

    System.Threading.Tasks.Parallel.Invoke(Action[] actions) at MethodInvoke2()
    InnerExceptionMessage :Object reference not set to an instance of an object.

    Hi sarvesh,

    Thank you post the issue to asp.net forum.

    The code you post above looks fine. According to the error message, I think the error occurs in the method "MethodInvoke2()".

    As other member suggested, you can check the StackTrace of AggregateException.

    try
                {
                    sw.Start();
                    Task.Factory.StartNew(() =>
                    {
                        Console.WriteLine("Cancellation in 5 sec.");
                        Thread.Sleep(5000);
                        settings.CancellationToken.Register(() => cancelNotification());
                        cts.Cancel();
                        Console.WriteLine("Canceled requested");
                    });
                    Parallel.Invoke(settings, () => MethodInvoke1(settings.CancellationToken),
                    () => MethodInvoke2(settings.CancellationToken));
    
                    sw.Stop();
                    Console.WriteLine("Total Time" + sw.Elapsed.Seconds);
                    Console.WriteLine("Ready");
                    Console.ReadLine();
                }
                catch (AggregateException ae)
                {
                    Console.WriteLine(ae.InnerException.StackTrace);
                    Console.WriteLine(ae.InnerException.Message);
                   // Console.WriteLine(ae.Message);
                    Console.WriteLine(ae.StackTrace);
                    Console.ReadLine();
                }

    Besides, I'd like to suggest you debuging the method "MethodInvoke2()" and find if you have used members of a null object.

    And also make sure the httprequest not return null object due to timeout. Thanks.

    Best Regards!

    Tuesday, December 24, 2013 1:30 AM