Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a piece of code.
C#
class Program
    {
        static void Main(string[] args)
        {
            CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
            CancellationToken token = cancellationTokenSource.Token;
            Task task = Task.Run(() =>
            {
                while (!token.IsCancellationRequested)
                {
                    Console.Write('*');
                    Thread.Sleep(1000);
                }
                token.ThrowIfCancellationRequested();
            }, token).ContinueWith((t)=>
                {
                    t.Exception.Handle((e)=>true);
                    Console.WriteLine("You have canceled the task");
                }, TaskContinuationOptions.OnlyOnCanceled); ;
            try
            {
                Console.WriteLine("Press enter to stop the task");
                Console.ReadLine();
                cancellationTokenSource.Cancel();
                task.Wait();
            }
            catch (AggregateException e)
            {
                Console.WriteLine(e.InnerExceptions[0].Message);
            }
            Console.WriteLine("Press enter to end the application");
            Console.ReadLine();
        }
    }

I got an exception "Object reference not set to an instance of an object" when hit enter key. I know it is a very common simple and stupid error.

If I remove task.Wait() then the error will go away but just can't figure out why.
Posted
Comments
[no name] 6-Jan-15 11:09am    
Because you trhow this exception by yourself after the first "whle".
Sergey Alexandrovich Kryukov 6-Jan-15 17:25pm    
This "throw" is done for a good reason, really. It only should be handled properly...
—SA
[no name] 7-Jan-15 3:44am    
Thank you for the hint. I try to understand the code...until now without big success :-)
Bruno
Sergey Alexandrovich Kryukov 6-Jan-15 17:26pm    
Is Sleep(1000) only for debugging purposes? For other purposes, it would be pointless...
Anyway, you should explain more, and start with what you want to achieve, exactly. The idea looks good, but I can only guess...
—SA
[no name] 7-Jan-15 3:42am    
Sorry I lead you to a wrong direction. Please read the comments of Sergey in your question.

Okay, figure it out.
Move
C#
token.ThrowIfCancellationRequested();

to a proper position.
C#
Task task = Task.Run(() =>
           {
               while (!token.IsCancellationRequested)
               {
                   Console.Write('*');
                   Thread.Sleep(1000);
               }

           }, token).ContinueWith((t)=>
               {
                   t.Exception.Handle((e)=>true);
                   Console.WriteLine("You have canceled the task");
               }, TaskContinuationOptions.OnlyOnCanceled);
           token.ThrowIfCancellationRequested();
           try
           {
               Console.WriteLine("Press enter to stop the task");
               Console.ReadLine();
               cancellationTokenSource.Cancel();
               //task.Wait();
           }
           catch (AggregateException e)
           {
               Console.WriteLine(e.InnerExceptions[0].Message);
           }
           Console.WriteLine("Press enter to end the application");
           Console.ReadLine();
 
Share this answer
 
Comments
[no name] 6-Jan-15 11:26am    
Now you can uncomment //Task.Wait(); :-)
[no name] 6-Jan-15 11:35am    
But I just found "You have canceled the task" is still not displayed. Something is still wrong.
C#
class Program
    {
        static void Main(string[] args)
        {
            CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
            CancellationToken token = cancellationTokenSource.Token;
            Task task = Task.Run(() =>
            {
                while (true)
                {
                    token.ThrowIfCancellationRequested();
                    Console.Write('*');
                    Thread.Sleep(1000);
                }
               
            }, token).
            ContinueWith((t) =>
            {
                Console.WriteLine("You have canceled the task");
            }, TaskContinuationOptions.OnlyOnCanceled);
           
            try
            {
                Console.WriteLine("Press enter to stop the task");
                Console.ReadLine();
                cancellationTokenSource.Cancel();
            }
            catch (AggregateException e)
            {
                Console.WriteLine(e.InnerExceptions[0].Message);
            }
            task.Wait();
            Console.WriteLine("Press enter to end the application");
            Console.ReadLine();
        }
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900