locked
Unable to dispose task RRS feed

  • Question

  • User1898849233 posted

    Hi All,

    I am using  Task Factory to make threaded request to multiple WCF Services. I want to dispose some tasks after  few seconds irrespective of their state, i. e. whether they have competed or not. Right now, if I want to dispose any task which is in running state throws an error and doesn't allow to dispose the task.

    Please go through the following code:

                Task<double>[] tasks2 = new Task<double>[2];
                CancellationToken[] CT = new CancellationToken[2];
                CancellationToken cts = new CancellationToken();
                CancellationTokenSource tokenSource = new CancellationTokenSource();
                CancellationToken token = tokenSource.Token;
                Stopwatch sw = new Stopwatch();
                sw.Start();
                tasks2[0] = Task<double>.Factory.StartNew(() => TrySolution1(),CT[0]);
                tasks2[1] = Task<double>.Factory.StartNew(() => TrySolution2(token),CT[1]);

                double g = tasks2[0].Result;
                CT[0].WaitHandle.Dispose();
                double h ;
                if (tasks2[1].Wait(10000))
                     h = tasks2[1].Result;  
                    
                else
                {
                    tokenSource.Cancel(); // cancel task
                   
                    h = 42; // default value
                }

                CT[1].WaitHandle.Dispose();
                tokenSource.Dispose();
              
                cts.WaitHandle.Dispose();         

                foreach (var item in tasks2)
                {
                    item.Dispose();
                }  
                sw.Stop();
                Console.WriteLine("task 0 completed first with resut of {0}", g);
                Console.WriteLine("task 1 completed second with result of {0}", h);
                Console.WriteLine("Time complete total with result {0}", sw.Elapsed);

                Console.ReadLine();

    static double TrySolution1()
            {
                //int i = rand.Next(1000000);
                // Simulate work by spinning
                Thread.SpinWait(10000);
                return DateTime.Now.Millisecond;
            }
            static double TrySolution2(CancellationToken token)
            {
                if (token.IsCancellationRequested)
                    token.ThrowIfCancellationRequested();
                   // throw new OperationCanceledException(token); // acknowledge cancellation
                   // token.ThrowIfCancellationRequested(); // OperationCanceledException
                   

               // int i = rand.Next(1000000);
                // Simulate work by spinning
                Thread.Sleep(100000);
                //Thread.SpinWait(1000000);
                return DateTime.Now.Millisecond;
            }

    Thank You

    Prashant

    Wednesday, October 23, 2013 7:00 AM

Answers

All replies