Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi!

How do I abort completed tasks. I have used the following code: -

C#
class Program
   {
       static void Main(string[] args)
       {
           System.Timers.Timer myTimer = new System.Timers.Timer();
           myTimer.Elapsed += new ElapsedEventHandler(OnTimer);
           myTimer.Interval = 1000;
           myTimer.Enabled = true;
           myTimer.AutoReset = false;
           Console.WriteLine(@"Press 'q' and 'Enter' to quit...");

           while (Console.Read() != 'q')
           {
               Thread.Sleep(1000);
           }
       }

       public static void OnTimer(Object source, ElapsedEventArgs e)
       {
           System.Threading.Tasks.Task[] tasks = new System.Threading.Tasks.Task[10];
           for(int i=0; i<10; i++)
           {

               if (tasks[i] != null && (tasks[i].Status == TaskStatus.Running ||      tasks[i].Status == TaskStatus.WaitingToRun || tasks[i].Status == TaskStatus.WaitingForActivation))
               {
                   Console.WriteLine("Task has attempted to start while already running");
               }

               else
               {
                   Console.WriteLine("Task {0} has begun", i);
                   Thread.Sleep(1000);
               }

               if (tasks[i].Status == TaskStatus.RanToCompletion)
               {
                   Console.WriteLine("\n\n\t");
                   Console.WriteLine("Task{0} has completed", i);
               }
           }

           Console.WriteLine();
           Console.WriteLine();

           System.Timers.Timer theTimer = (System.Timers.Timer)source;
           theTimer.Interval += 1000;
           theTimer.Enabled = true;
       }
  }

The moment the control reaches on the condition if(tasks[i].Status == TaskStatus.RanToCompletion) it throws an error "Object reference not set to an instance of an object". Can anyone tell me what's wrong with this code.

Second thing, how do i abort the completed tasks?
Posted
Updated 9-Apr-15 19:06pm
v2
Comments
Sergey Alexandrovich Kryukov 10-Apr-15 2:08am    
Why aborting a task if it is completed? As to the exception, check all the objects involved for null.
—SA
Sinisa Hajnal 10-Apr-15 3:03am    
Set a breakpoint at the line in question and check each object before you run the line.
aarif moh shaikh 10-Apr-15 3:57am    
The task status is null ...

1 solution

You have created an array to hold tasks, but not actually put any tasks in it.
Therefore you have an array of { null, null, null, null, null, null, null, null, null, null}

In your first test you have
if (tasks[i] != null &&
so the rest of the if does not get evaluated.

Then it gets to the second one and tries to test what is actually in tasks[I]...but nothing is in there so it can't test it.
 
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