Deleting a task and then Registering it again gives FileNotFound exception

Jun 30, 2011 at 12:28 AM

I'm working on a project to remove tasks (so we can be sure they don't trigger during the second step), edit/copy/remove some files, and restore the task, and so I have a bit of code that looks like so:

string taskName = "ExampleTask";

using{TaskService ts = new TaskService())


Task t = ts.GetTask(taskName);

TaskDefinition td = t.Definition;


... And the code goes on to do the actual work.

Then later:

ts.RootFolder.RegisterTaskDefinition(taskName, td);


Is the point where the exception is thrown from IPersist in the Win32 library.  The odd part is that if I change taskName before re-registering the Task it works fine, however it breaks other applications that depend on knowing about this Task's status and is thus Not A Valid Solution.  I've tried breaking out of the first using block and instantiating a second TaskService, that doesn't change anything.  The value of td isn't null when the exception is thrown, so its not like we accidentally lost the value, if I enumerate the Tasks after deleting it, it's gone, and I have no problem creating a new Task with any other name I want aside from the one I deleted, what gives?

Jun 30, 2011 at 5:08 AM

This is an oddity with the V1 COM objects and how the library is holding on to their instance. This will only show up on XP/WS2003 and prior Windows versions. This is an awkward work-around, but should work until I can figure out a fix. Replace your last line with the following:

t = ts.RootFolder.RegisterTaskDefinition(taskName + "2", td);
td = t.Definition;
ts.RootFolder.DeleteTask(taskName + "2");
ts.RootFolder.RegisterTaskDefinition(taskName, td);
Jun 30, 2011 at 5:11 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.