how to delete cancel build task item

Hi,

  Does anyone succeed in deleting all the "Task List" items after cancelling a build I cancel a build in the "OnBuildBegin" handler. The code I'm using to delete the task list items :

Window taskListWindow = ApplicationObject.Windows.Item( Constants.vsWindowKindTaskList );
TaskList taskList = (TaskList)taskListWindow.Object;
try
{
foreach(TaskItem item in taskList.TaskItems)
{
item.Delete();

}

This code is called in the "OnBuildDone" event handler. When item.Delete() is executed, I get an "Object reference not set to an instance of an object". The item reference, however, does exist. It contains the "Compilation cancelled by user" message.
  I am using Visual Studio 2003.
  All feedback is highly appreciated !

Thanks,
Bart



Answer this question

how to delete cancel build task item

  • ZokaBL

    Hello Bart,

    I just tried your code (same code without the Try statement) on VS2005 and it seems to work fine. I tried this inside the OnConnection() method of an Add-In (and not in an event handler).

    Does your code work outside of event handler can you check your task list code seperately by adding a sample task and deleting it

  • CLG3

    Hi Josh,

      I agree, your explanation conforms with the description I've found in the book "Inside Visual Studio".
      When a build of a solution is done, somehow, Visual Studio clears the task list. I realy wonder how this is done.

    Thanks for your time,
    Bart

  • Will Mavis



    foreach(TaskItem item in taskList.TaskItems)
    {
    item.Delete();
    }

     


    You are deleting items from the TaskList while iterating the list.  That invalidates the iterator.  I bet the null reference error you get is on the second loop through.

    I had some code like this I had to maintain, what I did was capture a list of the task items I need to delete to another collection (such as an ArrayList), and then iterate that. 

    Another thing to watch out for when you are deleting items in the task list---make sure the user hasn't selected one of them for editing!  In VS 2003, you could crash the environment if you deleted a task item that was currently being edited.

    My workaround for avoiding this was to check if the TaskList was the currently active window.  (It would have to be if the user is editing an item in it).  If it is, I simply activate a different window and then re-activate the task list.

    Hope that helps.



  • cheong00

    The task provider that created the tasks can remove them, of course.  That's how they are managed internally.  If you were to create your own task provider, you could add and remove your own tasks as much as you want, but no one else would be able to remove your tasks unless you decide to allow it.

    -Josh

  • latdev

    Hi Tim,

      Thanks for the feedback !
      Unfortunately, storing the TaskItem references in an ArrayList and calling delete on those references doesn't work : the null reference error is still there. 
      As a matter of fact, there is only one TaskItem in my TaskList.

    Thanks,
    Bart

  • Niko Pamboukas

    Hi Sudheer,

      Thanks for your effort !
      I've followed your suggestion and moved the code outside the event handler. Sample (cutom) tasks are deleted as expected. Any other task (for instance a TODO task) is not deleted from the list of task items. Deleting non-custom tasks always generates an "Object reference not set to an instance of an object".
      Note that I am using VS2003.

    Thanks,
    Bart
     

  • ailin

    Hi Josh,

      Thank you very much !! Big Smile

    Regards,
    Bart

  • GBR

    The task needs to support deletion.  Some built-in task types (such as User Tasks) do support deletion, others don't.  If you can't delete the task by selecting it and pressing the Delete key, you won't be able to delete it programmatically either.

    -Josh

  • how to delete cancel build task item